Search code examples
c++c++11static-membersdecltypestatic-assert

C++: name lookup in definition of class static variable in initializer


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?


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)