Reputation: 4366
Take the following snippet
#include <iostream>
using namespace std;
template<int i, typename T = int> struct A
{
T num = i;
A<i, T>()
{
cout << "Instantiated a A<" << i << ">" << endl;
}
};
template<int i, int i2> struct B
{
static A<i> a;
static A<i * i2> a2;
};
template<int i, int i2> A<i> B<i, i2>::a{};
template<int i, int i2> A<i * i2> B<i, i2>::a2{};
template<typename T> struct C
{
static void doSomething()
{
cout << "Have a A<" << T::a.num << "> and a A<" << T::a2.num << "> in C" << endl;
}
};
int main() {
typedef C<B<2, 2>> c;
cout << "Typedefined a C\nCalling static member function to initialize C<B<2, 2>>'s B<2, 2>'s A<>s" << endl;
c::doSomething();
return 0;
}
Now with gcc, then this compiles (both C++11 and C++14) and instantiates a
and a2
as expected.
Thanks to WhozCraig, this also compiles with clang.
However with Visual C++ (2015 edition), I get a parse error.
main.cpp(37) error C2143: syntax error: missing ';' before '<end Parse>'
Followed by some notes
main.cpp(19): note: while compiling class template static data member 'A<2,int> B<2,2>::a'
main.cpp(26): note: see reference to class template instantiation 'B<2,2>' being compiled
main.cpp(25): note: while compiling class template member function 'void C<B<2,2>>::doSomething(void)'
main.cpp(33): note: see reference to function template instantiation 'void C<B<2,2>>::doSomething(void)' being compiled
main.cpp(33): note: see reference to class template instantiation 'C<B<2,2>>' being compiled
What is going on here?
Upvotes: 0
Views: 246
Reputation: 32524
I was able to reduce the test case, identify the problem (which appears to be a bug in the Visual C++ compiler) and find a workaround:
#include <iostream>
using namespace std;
template<int i> struct A
{
int num = i;
A() {}
};
template<int i> struct B
{
static A<i> a;
};
// MSVC doesn't like this syntax
// |||
// vvv
template<int i> A<i> B<i>::a{};
// To fix the error, rewrite the above line in one of the below ways:
//
// template<int i> A<i> B<i>::a;
// template<int i> A<i> B<i>::a = {};
int main() {
typedef B<2> B2;
cout << B2::a.num << endl;
return 0;
}
Upvotes: 4