문제

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?

도움이 되었습니까?

해결책

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)

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top