C++ name scoping question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

C++ name scoping question

Roman Popov via cfe-dev
(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.
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.

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);

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.


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
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: C++ name scoping question

Roman Popov via cfe-dev
On Fri, Feb 3, 2017 at 5:39 PM, David Fontaine via cfe-dev <[hidden email]> 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
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: C++ name scoping question

Roman Popov via cfe-dev

​Thanks for the help Hubert.  Stating that the pFn declaration contains a function declarator certainly makes life easier.


-David



From: Hubert Tong <[hidden email]>
Sent: Friday, February 3, 2017 3:03 PM
To: David Fontaine
Cc: [hidden email]
Subject: Re: [cfe-dev] C++ name scoping question
 
On Fri, Feb 3, 2017 at 5:39 PM, David Fontaine via cfe-dev <[hidden email]> 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
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev