헤더에있는 상수에 따라 템플릿이 있습니다. 이런 식으로 뭔가 :템플릿에 사용되는 상수를 선언하는 방법은 무엇입니까?
상수를 정의하는 헤더 :
// header1.hpp const int CONST_VALUE1 = 10;
내가 템플릿이 헤더 :
// 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'
해결 방법이 있습니까? 아니면 header2.hpp에 header1.hpp를 포함시켜야합니까?
CONST_VALUE2이 (가) extern이기 때문에 작동하지 않습니까? – vulkanino
@vulkanino 예, 컴파일러가 말하는 것입니다. 해결 방법이 있습니까? –
대답은 여기에 있습니다 : http://stackoverflow.com/questions/643763/what-are-the-requirements-for-c-template-parameters – vulkanino