この例がコンパイルされない理由を理解しようとしています。私の理解では、静的変数が明示的に設定されていない場合、デフォルトで 0 になります。以下の 5 つの例では、そのうちの 4 つが期待どおりに動作しますが、コメントアウトされたものはコンパイルされません。
#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;
}
orint Bar<2>::i
と同じことをしないのはなぜですか?int Foo::i
static int i
編集: template<> を Bar<2> および Bar<3> 宣言に追加するのを忘れていました。(ただし、問題は解決しませんが、まだリンカー エラーが発生します)