How to determine the specialization of a member of a template class with initialization by default constructor?

The following program does not call the default constructor, as we would like:

#include <iostream> struct A { A() { std::cout << "Default constructor\n"; }; A(const A&) = delete; }; template<typename T> struct B { static T x; }; template<> A B<A>::x; // не вызывает конструктор int main() { } 
  • 2
    Although for some reason the ancient C ++ 98 is in the tags, the question itself is = delete from C ++ 11, so I mean that the question is still in modern C ++ - VTT
  • See en.stackoverflow.com/a/934161/182825 . Option number 2: "Explicit specialization of a static member. It is defined only if there is an initializer ." You have no initializer. In your case, you need {} or = {} . - AnT

1 answer 1

In the question code A B<A>::x; is an ad. In order to define this field, you must use the list initialization syntax:

 template<> A B<A>::x{}; 

17.8.3 Explicit specialization [temp.expl.spec]
14 includes a initializer; otherwise, it is a declaration. It is necessary to use a braced-init-list.
template<> X Q<int>::x; // declaration
template<> X Q<int>::x (); // error: declares a function
template<> X Q<int>::x { }; // definition
—End note]