Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pow (power) template implementation from wiki [duplicate]

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?

like image 541
user1494506 Avatar asked Jul 14 '12 12:07

user1494506


1 Answers

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

like image 102
TemplateRex Avatar answered Oct 13 '22 23:10

TemplateRex