模板元编程 - 使用 Enum Hack 和 Static Const 的区别

2021-12-13 00:00:00 templates metaprogramming c++

我想知道在使用模板元编程技术时使用静态常量和枚举黑客有什么区别.

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

EX:(斐波那契通过 TMP)

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;
};

对比

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 };
};

为什么要使用一个?我已经读到在类内部支持静态常量之前使用了 enum hack,但为什么现在使用它?

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?

推荐答案

枚举不是 lval,静态成员值是,如果通过引用传递模板将被实例化:

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.

boost 编码指南和旧线程 在有关该主题的 boost 档案中.

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

相关文章