Common enum
data is widely used in metaprogramming a template (see Boost.MPL )
#include <iostream> template <int N> struct Factorial { enum { value = N * Factorial<N - 1>::value }; }; template <> struct Factorial<0> { enum { value = 1 }; }; int main() { auto const f10 = Factorial<10>::value; // done at compile-time std::cout << f10 << "\n"; // prints 3628800 return 0; }
Conclusion on Ideone . The reason enum
used publicly here is because the structure is just a vehicle for transferring intermediate calculations to the top-level caller, and there is no need for encapsulation. This is a crude form of evaluating a compile-time function, where the struct
acts as the scope of the function and enum
as the return value.
source share