views:

508

answers:

2

I'm wondering what the difference is between using a static const and an enum hack when using template metaprogramming techniques.

EX: (Fibonacci via TMP)

template< int n > struct TMPFib {
  static const int val =
    TMPFib< n-1 >::val + TMPFib< n-2 >::val;
};

template<> struct TMPFib< 1 > {
  static const int val = 1;
};

template<> struct TMPFib< 0 > {
  static const int val = 0;
};

vs.

template< int n > struct TMPFib {
  enum {
    val = TMPFib< n-1 >::val + TMPFib< n-2 >::val
  };
};

template<> struct TMPFib< 1 > {
  enum { val = 1 };
};

template<> struct TMPFib< 0 > {
  enum { val = 0 };
};

Why use one over the other? I've read that the enum hack was used before static const was supported inside classes, but why use it now?

+8  A: 

For some the former one may seem less of a hack, and more natural. Also it has memory allocated for itself if you use the class, so you can for example take the address of val.

The latter is better supported by some older compilers.

Kornel Kisielewicz
Thanks. Exactly the kind of answer I was looking for.
Anonymous
Personally I completely disagree. Then enum version seems more natural. Why is there a need for a physical variable? Enum is the representation of a const value. Using a static const int seems more like taking a step backwards to the time when we had to use macros to represent constant values.
Martin York
@Martin, what has the keyword enumeration to do with calculation? But point taken -- everyone can have their own point on that.
Kornel Kisielewicz
@Martin - to be honest, I prefer using enums for this myself.
Kornel Kisielewicz
I'm an enum guy too. An enum is essentially just a named integral constant, which seems to fit our needs in these cases just fine. It's an rvalue with no storage allocate for it. A `static const int` has storage allocated for it and and everything, which isn't really necessary.
jalf
+12  A: 

Enums aren't lvals, static member values are and if passed by reference the template will be instanciated:

void f(const int&);
f(TMPFib<1>::value);

If you want to do pure compile time calculations etc. this is an undesired side-effect.

The main historic difference is that enums also work for compilers where in-class-initialization of member values is not supported, this should be fixed in most compilers now.
There may also be differences in compilation speed between enum and static consts.

There are some details in the boost coding guidelines and an older thread in the boost archives regarding the subject.

Georg Fritzsche