简体   繁体   English

如何初始化一个长度等于 const int 方法返回的数组?

[英]How can I initialize an array of its length equal to the return of a const int method?

What I'm trying to accomplish is this:我想要完成的是:

inline const int size() { return 256; }

int main()
{
    int arr[size()];

    return 0;
}

But Visual Studio gives me an error when initializing arr[size()]:但是在初始化 arr[size()] 时,Visual Studio 给了我一个错误:

expression must have a constant value表达式必须有一个常量值

Is there a way to accomplish what I want without using global variables, Macros, std::vector or creating arr[ ] on heap?有没有办法在不使用全局变量、宏、std::vector 或在堆上创建 arr[ ] 的情况下完成我想要的?

Drop the inline and const and add constexpr specifier instead, to solve the issue:删除inlineconst并添加constexpr说明符,以解决问题:

constexpr int size() { return 256; }

now you can use it as array size like:现在您可以将其用作数组大小,例如:

int arr[size()];

In C++ (not C) length of arrays must be known at compile time.在 C++(不是 C)中,arrays 的长度必须在编译时知道。 const qualifier only indicates that a value must not change during running time of a program. const限定符仅表示在程序运行期间不得更改值。

By using constexpr you will specify that the output of the function is a known constant, even before the program execution.通过使用constexpr ,您将指定 function 的 output 是已知常量,甚至在程序执行之前。

Looks like error message is confusing you.看起来错误消息让您感到困惑。 The problem is you do not need a constant value as message says but constant expression :问题是您不需要消息所说的constant value ,而是 常量表达式

Defines an expression that can be evaluated at compile time.定义一个可以在编译时计算的表达式。

Such expressions can be used as non-type template arguments, array sizes, and in other contexts that require constant expressions, eg此类表达式可以用作非类型模板 arguments,数组大小,以及其他需要常量表达式的上下文,例如

and to achieve that you can add constexpr specifier:为了实现这一点,您可以添加constexpr说明符:

The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. constexpr 说明符声明可以在编译时评估 function 或变量的值。 Such variables and functions can then be used where only compile time constant expressions are allowed (provided that appropriate function arguments are given).然后可以在仅允许编译时常量表达式的情况下使用此类变量和函数(前提是给出了适当的 function arguments)。

to your function allowing compiler to evaluate it as constant expression到您的 function 允许编译器将其评估为constant expression

 constexpr const int size() { return 256; }

Note: declaring return type by value as const does not really make any difference so you can simply remove that.注意:按值声明返回类型为const并没有任何区别,因此您可以简单地删除它。

You can replace const with constexpr as others have pointed out, but I should add that your original code compiles fine using the G++ compiler because it supports variable-length arrays, unlike Microsoft's compiler.正如其他人指出的那样,您可以用constexpr替换const ,但我应该补充一点,您的原始代码使用 G++ 编译器可以很好地编译,因为它支持可变长度 arrays,这与 Microsoft 的编译器不同。

From C++20, you can also make size an immediate function, ie a function that is guaranteed to be evaluated at compile time.从 C++20 开始,您还可以立即将size设为 function,即保证在编译时评估的 function。

consteval int size() { return 256; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM