Template Metaprogramming – Difference Between Using Enum Hack and Static Const

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.

Leave a Comment