是否可以使用const变量而不是constexpr来声明数组?

时间:2022-07-01 02:41:56

Is this C++ code correct?

这个C ++代码是否正确?

const size_t tabsize = 50;
int tab[tabsize];

The problem is that I've already seen numerous conflicting opinions on that matter. Even people at ##c++ IRC channel and programming forums claim radically different things.

问题是我已经在这个问题上看到了许多相互矛盾的意见。即使是## c ++ IRC频道和编程论坛的人也要求完全不同的东西。

Some people say the above code is correct.

有人说上面的代码是正确的。

Others argue that it is not, and that it should necessarily be like this:

其他人认为它不是,它应该是这样的:

constexpr size_t tabsize = 50;
int tab[tabsize];

Since I'm already confused enough by conflicting opinions of "C++ experts", could I please ask for a reasonably backed up answer? Many thanks!

由于我对“C ++专家”的观点存在冲突已经足够困惑了,我可以请一个合理的备份答案吗?非常感谢!

1 个解决方案

#1


9  

In C++ constant integers are treated differently than other constant types. If they are initialized with a compile-time constant expression they can be used in a compile time expression. This was done (in the beginning of C++, when constexpr didn't exist) so that array size could be a const int instead of #defined (like you were forced in C):

在C ++中,常量整数的处理方式与其他常量类型不同。如果使用编译时常量表达式初始化它们,则可以在编译时表达式中使用它们。这是完成的(在C ++的开头,当constexpr不存在时),因此数组大小可以是const int而不是#defined(就像你在C中被强制一样):

(Assume no VLA extensions)

(假设没有VLA扩展)

const int s = 10;
int a[s];          // OK in C++

const int s2 = read(); // assume `read` gets a value at run-time
int a2[s2];       // Not OK

int x = 10;
const int s3 = x;
int a3[s3];       // Not OK

So the answer is yes, you can use a const integer variable as the size of an array if it was initialized by a compile time constant expression

所以答案是肯定的,你可以使用const整数变量作为数组的大小,如果它是由编译时常量表达式初始化的


This is my answer from another question. That question is about int vs float const and constexpr, so not exactly a duplicate, but the answer applies here very nicely.

这是我对另一个问题的回答。那个问题是关于int vs float const和constexpr,所以不完全重复,但答案非常适用。

#1


9  

In C++ constant integers are treated differently than other constant types. If they are initialized with a compile-time constant expression they can be used in a compile time expression. This was done (in the beginning of C++, when constexpr didn't exist) so that array size could be a const int instead of #defined (like you were forced in C):

在C ++中,常量整数的处理方式与其他常量类型不同。如果使用编译时常量表达式初始化它们,则可以在编译时表达式中使用它们。这是完成的(在C ++的开头,当constexpr不存在时),因此数组大小可以是const int而不是#defined(就像你在C中被强制一样):

(Assume no VLA extensions)

(假设没有VLA扩展)

const int s = 10;
int a[s];          // OK in C++

const int s2 = read(); // assume `read` gets a value at run-time
int a2[s2];       // Not OK

int x = 10;
const int s3 = x;
int a3[s3];       // Not OK

So the answer is yes, you can use a const integer variable as the size of an array if it was initialized by a compile time constant expression

所以答案是肯定的,你可以使用const整数变量作为数组的大小,如果它是由编译时常量表达式初始化的


This is my answer from another question. That question is about int vs float const and constexpr, so not exactly a duplicate, but the answer applies here very nicely.

这是我对另一个问题的回答。那个问题是关于int vs float const和constexpr,所以不完全重复,但答案非常适用。