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

2023-03-10C/C++开发问题
5

本文介绍了模板元编程 - 使用 Enum Hack 和 Static Const 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

这篇关于模板元编程 - 使用 Enum Hack 和 Static Const 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6