gpt4 book ai didi

c++ - 使用枚举与变量,存储非类型模板参数值。 (在 Int2Type 模板中)

转载 作者:搜寻专家 更新时间:2023-10-30 23:51:12 24 4
gpt4 key购买 nike

通过 Andrei Alexandrescu 的 Modern C++ Design,我无法理解使用未命名、无作用域的 enum 来存储非类型参数的原因。为什么不直接使用变量。

有什么优点吗?

template <int v>
struct Int2Type
{
enum { value = v }; //why not use int value = v; which compiles fine
};

额外(如果有帮助):该模板旨在用作“类型生成器”以在编译时选择不同的函数。

最佳答案

使用 enum 而不是 static const int 或 C++11 static constexpr int 的主要原因是 enum 给你 true prvalue,非常像字面量 int。当 ODR 问题出现时,这一点就变得很重要。

例如,下面的一段代码工作正常(完整示例):

void foo(const int& x) {
std::cout << "X: " << x;
}

struct V {
enum {value = 42; }
};

void bar() {
foo(V::value);
}

另一方面,以下结构定义表现出未定义的行为(违反 ODR):

struct V {
static const int value = 42;
// same with static constexpr int value = 42;
};

这样做的原因是对值的绑定(bind)引用(调用 foo 时)ODR - 在它是 const 成员时使用值。必须定义所有 ODR 使用的变量。

但是,由于引用不能绑定(bind)到枚举成员(和文字),因此创建了一个临时对象,引用绑定(bind)到该临时对象,因此无需定义任何内容。这是一个非常有用的功能。

关于c++ - 使用枚举与变量,存储非类型模板参数值。 (在 Int2Type<int v> 模板中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57365439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com