According to the C++ Standard, function parameter's name is parsed by a declarator-id, and a declarator-id can also be a qualified name. That means, the following code is perfectly valid (if I've understood the relevant sections from the Standard correctly):
template<class T>
struct Sample
{
int fun(int T::count); //T::count is qualified variable name
};
My question basically is, why would anyone write such code? In what situations, the use of qualified name (in function parameter-list) can be advantageous?
EDIT:
It seems I understood the sections incorrectly. Instead of the above code, we can probably write the following code (as per the C++ standard):
template<class T>
struct sample
{
void fun(int arr[T::count]);
};
gcc-4.3.4 compiles it perfectly. But then, I'm not totally satisfied, because T::count is not a parameter anymore (I guess).
As far as I understand your code is ill formed because
P.S: I am not 100% sure. Please correct me if I am wrong. :)
Read Items 31 and 32 from Exceptional C++ by Herb Sutter. Both the items deal with Koenig lookup and the Interface principle.
It's invalid. The syntax allows arbitrary declarators, but 8.3.5p8 says
Edit Another quote which syntactically constraints declarators (8.3p1, [dcl.meaning]):
So in a parameter declaration, you must not use qualified names.
Edit: In the edited form, the function parameter type decays to an
int*
, even before a test is being made whetherT::count
actually exists and is an integer constant. If you want an example where a qualified name in such a signature would do something meaningful, considerWhen
fun
gets called without parameter, the compiler needs to determine the default argument, which then fails ifT
does not have acount
member, or that cannot be converted toint
.It seems I understood the sections incorrectly. Instead of that code, we can probably write the following code (as per the C++ standard):
gcc-4.3.4 compiles it perfectly. But then, I'm not totally satisfied, because
T::count
is not a parameter anymore (I guess).