我有以下模板struct:
template<int Degree>
struct CPowerOfTen {
enum { Value = 10 * CPowerOfTen<Degree - 1>::Value };
};
template<>
struct CPowerOfTen<0> {
enum { Value = 1 };
};
Run Code Online (Sandbox Code Playgroud)
这将是这样使用的:
const int NumberOfDecimalDigits = 5;
const int MaxRepresentableValue = CPowerOfTen<NumberOfDecimalDigits>::Value - 1;
// now can use both constants safely - they're surely in sync
Run Code Online (Sandbox Code Playgroud)
现在该模板需要Degree非负面的.我想为此强制执行编译时断言.
我怎么做?我试图添加一个析构函数CPowerOfTen:
~CPowerOfTen() {
compileTimeAssert( Degree >= 0 );
}
Run Code Online (Sandbox Code Playgroud)
但由于它没有被直接调用,因此Visual C++ 9决定不实例化它,因此根本不评估编译时断言语句.
我怎么能强制执行编译时检查Degree非负面?