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?
A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.
Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.
Just like in case of the function arguments, template parameters can have their default values. All template parameters with a default value have to be declared at the end of the template parameter list.
The use of templates can be thought of as compile-time polymorphism. The technique is used by a number of languages, the best-known being C++, but also Curl, D, Nim, and XL.
template<bool> struct StaticCheck;
template<> struct StaticCheck<true> {};
template<int Degree>
struct CPowerOfTen : StaticCheck<(Degree > 0)> {
enum { Value = 10 * CPowerOfTen<Degree - 1>::Value };
};
template<>
struct CPowerOfTen<0> {
enum { Value = 1 };
};
Edit: without infinite recursion.
// Help struct
template<bool, int> struct CPowerOfTenHelp;
// positive case
template<int Degree>
struct CPowerOfTenHelp<true, Degree> {
enum { Value = 10 * CPowerOfTenHelp<true, Degree - 1>::Value };
};
template<>
struct CPowerOfTenHelp<true, 0> {
enum { Value = 1 };
};
// negative case
template<int Degree>
struct CPowerOfTenHelp<false, Degree> {}
// Main struct
template<int Degree>
struct CPowerOfTen : CPowerOfTenHelp<(Degree >= 0), Degree> {};
You can use a uint. You won't get a compile time error but at least it will be self-documenting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With