为什么模板声明中的 size_t 参数需要是 const ?

发布于 2024-08-29 18:20:29 字数 522 浏览 6 评论 0原文

我可以有

std::bitset< 10 > bitsetA;

任何问题,也

const size_t LengthB = 20;
std::bitset< LengthB > bitsetB;

可以没有任何问题。

但是,如果长度不是 const

size_t LengthC = 30;
std::bitset< LengthC > bitsetC;  // Line 30, say

我会遇到以下编译错误,

'LengthC' cannot appear in a constant-expression
template argument 1 is invalid

原因是什么?

如果接受第 30 行,对于编译器和用户代码来说会出现什么问题?是因为 LengthC 可能有一些别名吗?

I can have

std::bitset< 10 > bitsetA;

or

const size_t LengthB = 20;
std::bitset< LengthB > bitsetB;

without any problem.

But, if the length is not const

size_t LengthC = 30;
std::bitset< LengthC > bitsetC;  // Line 30, say

I face the following compilation error

'LengthC' cannot appear in a constant-expression
template argument 1 is invalid

What is the reason for that?

What would be the problem, for compiler and for user code, if line 30 was to be accepted? Is it because LengthC might have some alias?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走过海棠暮 2024-09-05 18:20:29

模板根据模板参数实例化新类型,这是在编译时完成的。您无法在运行时实例化新类型,因为 C++ 是静态类型的。

因此,当您有一个非常量变量时​​,它不能作为模板参数传递,因为不能保证它是该值(您必须“在运行时实例化一个新类型”)。只有当它是 const 时,您才能确保该值确实是常量,因此可以在模板参数中使用。

Templates instantiate new types based off their template parameters, which is done at compile-time. You cannot instantiate new types at run-time, because C++ is statically-typed.

So when you have a non-const variable, it can't be passed as a template parameter because it can't be guaranteed to be that value (you'd have to potentially "instantiate a new type at run-time"). Only when it's const are you ensured the value is indeed constant, and therefore usable in a template parameter.

翻了热茶 2024-09-05 18:20:29

模板参数必须在编译时声明const,以便模板可以在编译时实例化。

在您给出的示例中,确实显示 LengthC 从初始化点到必须实例化模板的点不会发生变化,因此可以将其视为常量,但编译器没有义务弄清楚这一点。规范规定参数必须声明为 const,以便不需要进行编译时流控制分析。

Template arguments have to be declared const at compile time so that the template can be instantiated at compile time.

In the example you give, it does indeed appear that LengthC isn't going to change from the point where it is initialized to the point where the template has to be instantiated, so it could be treated as constant, but the compiler is not obligated to figure that out. The spec says the arguments have to be declared const, so that no compile-time flow control analysis needs to be done.

兮子 2024-09-05 18:20:29

模板是编译时生物,变量是运行时生物。如果需要将变量传递给模板,则需要在运行时执行此操作,例如在模板类的构造函数中。

Templates are compile-time creatures - variables are run-time ones. If you need to pass a variable to a template, you need to do it at run-time, for example in a templated class's constructor.

猥︴琐丶欲为 2024-09-05 18:20:29

C++ 编译器在 const 转换方面严格

10const size_t LengthB = 20; 的计算结果都是常量。如果没有 const 关键字,编译器就无法轻松确定变量在声明和使用之间是否可能发生变化。

C++ compilers are strict when it comes to const casting.

Both 10 and const size_t LengthB = 20; evaluates to a constant. without the const keyword, the compiler cannot easily determine if the variable may change between declaration and usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文