简体   繁体   English

编译错误:使用constexpr声明std :: array的大小

[英]Compile Error: Use constexpr to declare std::array size

I am learning constexpr and, for my understanding, constexpr tells the compiler to calculate functions during compile time instead of running time. 我正在学习constexpr ,据我所知, constexpr告诉编译器在编译时而不是运行时计算函数。 I used the following code for testing but bumped into an error I really don't understand. 我使用以下代码进行测试,但遇到了我真的不明白的错误。 Can you please explain why? 你能解释为什么吗?

#include <iostream>
#include <array>
using namespace std;

constexpr int foo(int i)
{
    return i + 5;
}

int main()
{
    int i = 10;
    std::array<int, foo(5)> arr; // OK
    // But...
    std::array<int, foo(i)> arr1; // Error
}

The error is: the value of ' i ' is not usable in a constant expression. 错误是:' i '的值不能在常量表达式中使用。 Why? 为什么? i is declared beforehand why does it have to be a const ? i预先声明了为什么它必须是const

for my understanding constexpr tells the compiler to calculate functions during compile time instead of running time. 就我的理解而言,constexpr告诉编译器在编译时而不是运行时计算函数。

Not exactly: with constexpr the compiler can (not must) calculate function compile time. 不完全是:使用constexpr ,编译器可以(不是必须)计算函数的编译时间。 And the compiler do it when it's necessary and possible. 然后,编译器会在必要且可能的情况下执行此操作。

In case of 的情况下

std::array<int, foo(5)> arr; // OK

it's necessary (because the second template argument of std::array must be known at compile time) and possible (because 5 is known at compile time). 这是必要的(因为必须在编译时知道std::array的第二个模板参数)并且可能的(因为在编译时知道5)。

But with 但是随着

int i = 10;
std::array<int, foo(i)> arr1; // Error

it's necessary ( std::array ) but not possible (because i is a not-constant variable and the compiler can't use the i value compile time but only run time). 这是必需的( std::array ),但不可能(因为i是一个非恒定变量,并且编译器不能使用i值编译时间,而只能使用运行时)。

It's necessary but not possible, so the error. 这是必要的,但不可能,因此是错误。

But you can write 但是你可以写

int i { 10 };
int j { foo(i) };

because it's not possible call foo(i) compile time but it isn't necessary (because j can be initialized run time). 因为不可能调用foo(i)编译时,但这不是必需的(因为j可以初始化运行时)。 So foo(i) is called (supposedly) run time. 因此foo(i)被称为(据说)运行时。

To compile the std::array using foo(i) , you should define i as constexpr (or const ) 要使用foo(i)编译std::array ,应将i定义为constexpr (或const

constexpr int i { 10 };

so the compiler can use the value of i compile time. 因此编译器可以使用i编译时间的值。

Why? 为什么? i is declared beforehand why does it have to be a const? 我事先被声明了为什么它必须是一个const?

Short answer: because the C++11 standard say so. 简短的答案:因为C ++ 11标准这么说。

Long answer: because, in this way, it's simpler to construct a compiler. 长答案:因为以这种方式,构造编译器更简单。 If you want to use a value compile time, you can declare it as constexpr and the compiler check that it is never modified. 如果要使用值编译时间,则可以将其声明为constexpr ,然后编译器将检查它是否从未修改。 It's (relatively) simple to do. (相对)容易做到。

Otherwise, if you could use compile time the value of a not-constant variable, the compiler should follow the story of a variable to determine it's value when used in a constexpr function. 否则,如果您可以使用编译时非恒定变量的值,则编译器应遵循变量的故事,以确定在constexpr函数中使用变量时的值。 In your toy example is simple, in real life would be a nightmare. 在您的玩具示例中很简单,在现实生活中将是一场噩梦。

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

相关问题 使用 constexpr 作为 std::array 大小 - Using constexpr as std::array size 用 constexpr if 定义和声明一个 const std::array - Define and declare a const std::array with constexpr if 通过constexpr或模板函数在编译时获取多维std :: array的大小 - Get the size of multi dimensional std::array at compile time via constexpr or template function constexpr上下文中std :: array指针的size() - size() of std::array pointer in constexpr context 使用n维std :: array的大小初始化constexpr std :: array - Initialize a constexpr std::array with the size of an n-dimensional std::array 尝试从 std::array compile.data() 作为 c++20 中的 constexpr function 时出错 - error while trying to compile .data() from std::array as a constexpr function in c++20 C ++ constexpr:在编译时计算std数组 - C++ constexpr : Compute a std array at compile time 使用 std::acos 和 clang++ 而不是 g++ 的 Constexpr 编译错误 - Constexpr compile error using std::acos with clang++ not g++ 使用constexpr确定数组指针的大小 - use constexpr to determine size of array pointer 有没有办法声明一个特定大小的 std::vector 数组? - Is there a way to declare an array of std::vector of a certain size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM