C++ name scoping question

(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.

  1. 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

(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 for the help Hubert. Stating that the pFn declaration contains a function declarator certainly makes life easier.

-David