Possible Duplicate:
Template Metaprogramming - Difference Between Using Enum Hack and Static Const
please explain what for is enum
used in following implementation of power template.
template<int B, int N>
struct Pow {
// recursive call and recombination.
enum{ value = B*Pow<B, N-1>::value };
};
template< int B >
struct Pow<B, 0> {
// ''N == 0'' condition of termination.
enum{ value = 1 };
};
int quartic_of_three = Pow<3, 4>::value;
I found it on wikipedia. Is there a difference between int
and enum
in this case?
There could be a difference if you ever try to take an address of an static const int
. In that case, the compiler will generate storage for the static const int
. You can't take the address of an enum
and the compiler will never generate storage for it.
See also Template Metaprogramming - Difference Between Using Enum Hack and Static Const
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