Question

I have simple code:

#include <type_traits>

class A {
public:
    static int a;
};

void a() {}

int A::a = [](){static_assert(std::is_function<decltype(a)>::value,"'a' is not a function");return 777;}();

int main() {

    return 0;
}

During compilation (with g++ 4.8.1 and clang 3.4) a get static assert error about 'a' is not a function. But inside assert, in decltype I put 'a' (which is a function) not A::a. Shouldn’t compiler took a function (a) instead a class member (A::a)?

Can you give any reference to C++ specification where it is explained?

Était-ce utile?

La solution

Shouldn’t compiler took a function (a) instead a class member (A::a)?

No; the member definition is in the class scope, so unqualified lookup of a gives the member.

Can you give any reference to C++ specification where it is explained?

Class scope is defined in C++11 3.3.7; in particular:

The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions)

Autres conseils

The compiler will always look at the closes scope first, and as you are defining A::a the closest scope is the scope of the class A.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top