[cfe-dev] Eager instantiation of static data member

Stephan Bergmann via cfe-dev cfe-dev at lists.llvm.org
Fri Jan 4 02:17:55 PST 2019


I'm puzzled why Clang (up through recent trunk) rejects

> $ cat test.cc
> template<typename T> struct S { static const int n = T::n; };
> S<int> s;
> 
> $ clang++ -std=c++17 -c test.cc
> test.cc:1:54: error: type 'int' cannot be used prior to '::' because it has no members
> template<typename T> struct S { static const int n = T::n; };
>                                                      ^
> test.cc:2:8: note: in instantiation of template class 'S<int>' requested here
> S<int> s;
>        ^
> 1 error generated.

when C++17 [temp.inst]/3 states that "the initialization (and any 
associated side effects) of a static data member does not occur unless 
the static data member is itself used in a way that requires the 
definition of the static data member to exist."

Is the initializer instantiated to check whether it is a constant 
expression (as required by [class.static.data]/3)?  If yes, is the 
compiler indeed allowed/required to check that when instantiating S<int> 
here?

(A similar

> template<typename T> struct S { static constexpr int n = T::n; };
> S<int> s;

is accepted by Clang, FWIW.)



More information about the cfe-dev mailing list