[英]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.