[cfe-dev] C++ name scoping question

Hubert Tong via cfe-dev cfe-dev at lists.llvm.org
Fri Feb 3 15:03:16 PST 2017


On Fri, Feb 3, 2017 at 5:39 PM, David Fontaine via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> (Also trying cfe-dev. Originally sent this to cfe-users.)
>
> Hi,
>
> I'm hoping a C++ language expert can help me with scoping rules for named
> function parameters.
>
> As background, I'd like to allow named function parameters in non-function
> declarations (such as function-pointer-typed variables) to be referenced in
> attribute expressions.  Currently, attempting to do so results in "use of
> undeclared identifier".  However, I'm not even sure the standard allows me
> to do this, let alone what assumptions any patch to clang might run afoul
> of.
>
> Consider the declaration:
>
>     void (*pFn)(int x);
>
> Reading the standard (specifically, I'm looking at C++11 draft N4527), I
> gather that:
> ​1. pFn is not a "function declarator". I didn't see a clear definition of
> this, but I assume function and variable are mutually exclusive
> descriptions of a declarator, and that this would be a variable declarator.
>
Within a declaration, certain portions of the syntax are considered
declarators. The interpretation of this is that (*pFn)(int x) is the
function declarator here.


> 2. According to [basic.scope.proto] (3.3.4), named parameters inside
> function declarators which are not also definitions have function prototype
> scope, but this only applies to function declarators.  I do not find a
> scoping rule that would apply to x in my pFn example, assuming point 1 is
> correct.
>
The scoping rule you found applies.


>
> This seems like x has no applicable scoping rule and you shouldn't be able
> to reference it.  However, I note the following declaration is accepted as
> valid C++11:
>
>     auto (*pSum)(int x, double y) -> decltype(x + y);
>
(*pSum)(int x, double y) -> decltype(x + y) is the function declarator here.


>
> I'd appreciate any help understanding what portions of the standard apply
> to my examples and how to interpret them, and whether the standard in fact
> allows me to place an attribute on pFn which references x.
>
An attribute appertaining to the function type (placed after the optional
noexcept-specifier) is part of the function declarator.


>
>
> Thanks,
> David Fontaine
>
>
> ------------------------------
> This email message is for the sole use of the intended recipient(s) and
> may contain confidential information.  Any unauthorized review, use,
> disclosure or distribution is prohibited.  If you are not the intended
> recipient, please contact the sender by reply email and destroy all copies
> of the original message.
> ------------------------------
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170203/d9af0bca/attachment.html>


More information about the cfe-dev mailing list