I have the following boilerplate struct :
template<int Degree> struct CPowerOfTen { enum { Value = 10 * CPowerOfTen<Degree - 1>::Value }; }; template<> struct CPowerOfTen<0> { enum { Value = 1 }; };
which should be used as follows:
const int NumberOfDecimalDigits = 5; const int MaxRepresentableValue = CPowerOfTen<NumberOfDecimalDigits>::Value - 1;
Now the template requires Degree be non-negative. I would like to provide a compile time statement for this.
How can I do it? I tried adding a destructor to CPowerOfTen :
~CPowerOfTen() { compileTimeAssert( Degree >= 0 ); }
but since it is not called directly, Visual C ++ 9 decides not to instantiate it, so the compert-time assert statement is not evaluated at all.
How can I make the compile time check for Degree non-negative?
c ++ visual-c ++ templates metaprogramming
sharptooth
source share