I have a following templated struct
:
template<int Degree>
struct CPowerOfTen {
enum { Value = 10 * CPowerOfTen<Degree - 1>::Value };
};
template<>
struct CPowerOfTen<0> {
enum { Value = 1 };
};
which is to be used like this:
const int NumberOfDecimalDigits = 5;
const int MaxRepresentableValue = CPowerOfTen<NumberOfDecimalDigits>::Value - 1;
// now can use both constants safely - they're surely in sync
now that template requires Degree
to be non-negative. I'd like to enforce a compile-time assert for that.
How do I do that? I tried to add a destructor to CPowerOfTen
:
~CPowerOfTen() {
compileTimeAssert( Degree >= 0 );
}
but since it is not called directly Visual C++ 9 decides not to instantiate it and so the compile-time assert statement is not evaluated at all.
How could I enforce a compile-time check for Degree
being non-negative?