我有一個模板,它依賴於頭中的常量。是這樣的:如何聲明模板中使用的常量?
定義恆定的標頭:
// header1.hpp const int CONST_VALUE1 = 10;
其中I有一個模板頭:
// header2.hpp extern const int CONST_VALUE2; template< int N > struct A { }; struct B { // some member functions A<CONST_VALUE2> a; };
與B的定義和源極常數
// source2.hpp #include "header2.hpp" // implementation of B const int CONST_VALUE2 = CONST_VALUE1;
這當然不起作用。錯誤是這樣的:
error: the value of 'CONST_VALUE2' is not usable in a constant expression
note: 'CONST_VALUE2' was not initialized with a constant expression
note: in template argument for type 'int'
是否有解決方法?或者我必須將header1.hpp包含到header2.hpp中?
它不工作,因爲CONST_VALUE2是extern? – vulkanino 2012-02-22 10:37:10
@vulkanino是的,這就是編譯器說的。有沒有解決方法? – 2012-02-22 10:39:32
答案在這裏:http://stackoverflow.com/questions/643763/what-are-the-requirements-for-c-template-parameters – vulkanino 2012-02-22 10:40:28