Can an array be declared with a size that is a constant variable and not constexpr?

Is this C ++ code correct?

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

The problem is that I have already seen numerous conflicting opinions on this issue. Even people in the ## C ++ IRC channel and programming forums claim to have fundamentally different things.

Some say the code above is correct.

Others argue that this is not the case, and that it must necessarily be as follows:

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

Since I'm already quite confused by the conflicting opinions of “C ++ experts,” can I ask for a reasonable fallback answer? Many thanks!

+4
source share
1 answer

++ , . , . ( C++, constexpr ), const int #define d (, C):

( 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

, , const integer ,


. int vs float const constexpr, , .

+9

Source: https://habr.com/ru/post/1623577/


All Articles