依存関係が示されている次のコードがコンパイルされない理由を理解するのに苦労しているので、修正に役立てていただければ幸いです。
main.cpp
#include <cstdlib>
#include <iostream>
#include "Foo.h"
#include "Bar.h"
int main()
{
Foo<Bar> f1; // ERROR
Foo<Bar,true> f2; // works
return EXIT_SUCCESS;
}
Foo.h
template<typename T, bool S = T::HAS_NATIVE_SUPPORT>
struct Foo
{
};
Bar.h
struct Bar
{
static const bool HAS_NATIVE_SUPPORT;
};
Bar.cpp
#include "Bar.h"
const bool Bar::HAS_NATIVE_SUPPORT = true;
VisualStudio2008のコマンドプロンプトで次のエラーが発生します
cl main.cpp Bar.cpp
main.cpp(12) : error C2975: 'S' : invalid template argument for 'Foo', expected compile-time constant expression
c:\tmp\c++tests\so\Foo.h(1) : see declaration of 'S'
g ++(GCC)4.5.3では、次のエラーメッセージが表示されます。
$ g++ main.cpp Bar.cpp
main.cpp: In function ‘int main()’:
main.cpp:12:9: error: ‘Bar::HAS_NATIVE_SUPPORT’ is not a valid template argument for type ‘bool’ because it is a non-constant expression
main.cpp:12:12: error: invalid type in declaration before ‘;’ token