Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++11 not support name lookup like this? [closed]

struct A
{
    enum InnerEnum { X };

    A(InnerEnum x)
    {}
};

int main()
{
    A a(X);
}

The compiler complains: error C2065: 'X' : undeclared identifier

The compiler knows what the constructor's parameter type is, so when I pass X as the argument, the compiler should know it is a valid argument.

I know this is not ADL(Argument-dependent Name Lookup, also known as Koenig Lookup), but I think it's useful and pretty handy. Because I don't have to write as follows:

A a(A::X);

I think the ADL rule should be generalized to such a case.

Am I right?

like image 854
xmllmx Avatar asked Jan 04 '13 19:01

xmllmx


People also ask

What is name lookup in c++?

Name lookup is the procedure by which a name, when encountered in a program, is associated with the declaration that introduced it. For example, to compile std::cout << std::endl;, the compiler performs: unqualified name lookup for the name std , which finds the declaration of namespace std in the header <iostream>

Does C ++ 11 include C11?

No, C++11 does not support ALL the features of C11. It does not even support all the features of C99. Variable-length arrays, for example, were introduced in C99, but C++ does not yet support them.


1 Answers

Function calls in C++ are subject to function overload resolution. Overload resolution is driven by the argument types. I.e. the language "works" specifically in that direction: from argument types to specific version of the function with the given name.

You are proposing to introduce a reverse process - argument type deduction based on function name. This will not work in general case. It might work in cases when there's only one candidate function (as in your example), but, again, in is contrary to principles that work in the general situation when the function is overloaded.

Of course, the situation will get even more complicated when name lookup on unqualified name X can see something else named X in addition to your A::X. I think it can easily get very counterintuitive.

like image 100
AnT Avatar answered Sep 19 '22 11:09

AnT