consider following code:
#include <iostream> template <typename t> struct { t x; a(t x) : x(x) { } static const a; }; template <typename t> const a<t> a<t>::a(5); template <typename t> struct b : public a<t> { b(t x) : a<t>(x) { } static const b b; }; template <typename t> const b<t> b<t>::b(a<t>::a.x); int main() { //auto test0 = a<int>::a.x; auto test1 = b<int>::a.x; auto test2 = b<int>::b.x; auto test3 = a<int>::a.x; std::cout << "test1 = " << test1 << "\ttest2 = " << test2 << "\ttest3 = " << test3 << std::endl; return 0; }
running code may give following text:
test1 = 5 test2 = 5 test3 = 5
but not when running on visual studio 2015. vs produces:
test1 = 5 test2 = 0 test3 = 5
unless uncomment definition of test0 variable in main(). why?
looks msvc++ doesn't initialize base template struct static member, unless explicitly instantiate before using derived struct. related dynamic constant initialization, i'm not sure what's happening here.
how overcome behavior? need b<int>::b.x
equal b<int>::a.x
everytime, without forcing myself remember instantiate a<int>
first. hackish trickeries acceptable. thanks.