1

I'm learning now C++ I'm reading the book Effective C++ (Scott Meyers). In the book, there is an item about const variables, and I try to work with them. I notice something very interesting that I what to know if it bug in C++: (I'm working with C++98 standard)

void Test(const int i)
{
    int arr[i] = {0};
    
    for (int j = 0; i > j; ++j)
    {
        arr[j] = i;
    }
}

This function will compile and work exactly as I want (create int array on the stack with the size of 'i'. When I remove the 'const' from 'i' it won't compile. I try this on gcc and clang.

Edit: link to Compiler Explorer

  • I don't think that's standard, it might be a GCC/CLANG extension. In C++, array sizes have to be known at compile time. – Barmar Aug 20 '20 at 05:30
  • [This question](https://stackoverflow.com/questions/30476227/using-const-int-as-array-size) says they get an error for the same thing. – Barmar Aug 20 '20 at 05:33
  • Some compilers support the C Variable Length Arrays in C++. It is not a good idea to use it though. It interacts badly with other C++ features like templates and object creation and destruction. – Zan Lynx Aug 20 '20 at 05:36
  • @ZanLynx But if the compiler supports VLA it should work without `const` as well. – Barmar Aug 20 '20 at 18:40

2 Answers2

3

To catch this kind of mistake in the future the compiler flag you want for both g++ and clang++ is -pedantic. And always remember to specify your language standard or you don't know what you'll get.

$ g++ -std=c++98 -pedantic c++-vla.cpp -o c++-vla
c++-vla.cpp: In function ‘void f(size_t)’:
c++-vla.cpp:3:30: warning: ISO C++ forbids variable length array ‘g’ [-Wvla]
    3 | void f(const size_t x) { int g[x]; }
      |                              ^

$ clang++ -std=c++98 -pedantic c++-vla.cpp -o c++-vla
c++-vla.cpp:3:31: warning: variable length arrays are a C99 feature [-Wvla-extension]
void f(const size_t x) { int g[x]; }
                              ^
1 warning generated.
Barmar
  • 741,623
  • 53
  • 500
  • 612
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
1

First of all, const in your function signature is ignored by the compiler. So the following two are equivalent:

Test(const int i) {}
Test(int i) {}

Secondly, this isn't valid C++ regardless of whether it compiles or not:

int arr[i] = {0};

It isn't valid because i is not a compile time constant i.e., the value of i has to be known at the time of compilation.

Try on Compiler Explorer

Waqar
  • 8,558
  • 4
  • 35
  • 43
  • 2
    I think `const` in the function signature prevents assigning the variable inside the function. – Barmar Aug 20 '20 at 05:33
  • Yes, but to the compiler it doesn't make a difference whether it is `const` or not (for value types). – Waqar Aug 20 '20 at 05:36
  • 2
    It doesn't make a difference when deciding whether to allow it to be used as an array size, but it makes a difference for other things. – Barmar Aug 20 '20 at 05:38
  • I try it on compiler Compiler Explorer - [link](https://godbolt.org/z/4fz3ad) This compiled and work – Izik Avinoam Aug 20 '20 at 06:43
  • That's because of GCC's own extensions that make it work. Compile with `-pedantic` and then see what happens. Also, read this post for a bigger answer to why GCC allows this: https://stackoverflow.com/questions/39334435/variable-length-array-vla-in-c-compilers – Waqar Aug 20 '20 at 07:17
  • To the **caller** those two versions of `Test` are the same thing. Top-level `const` is ignored for function overloading. **Inside** the function they are not the same thing: in the first version, `i` cannot be modified; in the second, it can. – Pete Becker Aug 20 '20 at 13:47