Template Metaprogramming - Difference Between Using Enum Hack and Static Const(模板元编程 - 使用 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 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模板元编程 - 使用 Enum Hack 和 Static Const 的区别
基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
