I'm trying to figure out why this example doesn't compile. My understanding is that if a static variable is not explicitly set then it defaults to 0. In the five examples below four of them behave as I would expect, but the one that's commented out won't compile.
#include <iostream>
class Foo
{
public:
static int i;
static int j;
};
template <int n>
class Bar
{
public:
Bar(int) { }
static int i;
};
static int i;
int Foo::i;
int Foo::j = 1;
template <> int Bar<2>::i;
template <> int Bar<3>::i = 3;
int main(int argc, char** argv)
{
std::cout << "i " << i << std::endl;
std::cout << "Foo::i " << Foo::i << std::endl;
std::cout << "Foo::j " << Foo::j << std::endl;
//std::cout << "Bar<2>::i " << Bar<2>::i << std::endl; // Doesn't compile?
std::cout << "Bar<3>::i " << Bar<3>::i << std::endl;
return 0;
}
Why doesn't int Bar<2>::i
do the same thing as int Foo::i
or static int i
?
Edit: I had forgotten to add template<> to the Bar<2> and Bar<3> declarations. (doesn't solve the problem though, still getting linker errors)