且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何转换int到const int在堆栈分配数组大小?

更新时间:2023-02-05 20:48:04


我应该如何输入 n1 将它作为 const int

数组的大小必须是所谓的积分常数表达式(ICE)。该值必须在编译时可计算。一个 const int (或其他const限定的整数类型对象)只有在它本身用一个积分常数表达式初始化时才能用于积分常数表达式。

The size of the array must be what is called an Integral Constant Expression (ICE). The value must be computable at compile-time. A const int (or other const-qualified integer-type object) can be used in an Integral Constant Expression only if it is itself initialized with an Integral Constant Expression.

一个非const对象(如 n1 )不能出现在积分常数表达式的任何位置。

A non-const object (like n1) cannot appear anywhere in an Integral Constant Expression.

您是否考虑过使用 std :: vector< int>

[注意 - 演员是完全不必要的。以下两者完全相同:

[Note--The cast is entirely unnecessary. Both of the following are both exactly the same:

const int N = n1;
const int N = const_cast<const int&>(n1);

- 结束注]