6

Is this C++ code correct?

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.

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!

  • what you mean by `code is correct.` , syntactically or semantically? – tchelidze Jan 09 '16 at 17:35
  • @tchelidze I think both? –  Jan 09 '16 at 17:38
  • you can very easily check if code is correct syntactically, just paste in `Visual Studio` and open up `Error List`. – tchelidze Jan 09 '16 at 17:39
  • 3
    @tchelidze Because Visual Studio is well known for its standard conformance? (not really.) – T.C. Jan 09 '16 at 17:41
  • 2
    @tchelidze The fact the SOME COMPILER (or even ALMOST ALL COMPILERS) accept a code does not yet mean the code is correct as per the C++ Standard. –  Jan 09 '16 at 17:43
  • Anyway, `const` is correct, but it's pretty tedious to prove it from the standard, especially as the area has underwent some pretty big changes between versions. And the standardese is not exactly easy to read, either. – T.C. Jan 09 '16 at 17:43
  • @tchelidze: This will only check if it's syntactically correct for Visual C++ (and not even that, because AFAIK the IDE does not perform the same operations as the real compiler). – Christian Hackl Jan 09 '16 at 17:43
  • 1
    Start from [\[dcl.array\]/1](http://eel.is/c++draft/dcl.array#1), which leads you to [\[expr.const\]/4](http://eel.is/c++draft/expr.const#4), which leads to [\[expr.const\]/5](http://eel.is/c++draft/expr.const#5), which leads to [\[expr.const\]/2](http://eel.is/c++draft/expr.const#2). I'm too lazy to copy/paste the text and do all the formatting. – T.C. Jan 09 '16 at 17:45
  • @T.C. - Write that up as an answer – Martin Bonner supports Monica Jan 09 '16 at 17:47
  • Is it possible to apply a strict rule for _only use constexpr array bound_ on a compiler (e.g., mingw)? So that, the build process will fail – Gökhan Barış Aker Dec 16 '18 at 10:33

1 Answers1

16

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):

(Assume no VLA extensions)

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


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.

Community
  • 1
  • 1
bolov
  • 72,283
  • 15
  • 145
  • 224