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

4
  • 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. Commented Aug 20, 2020 at 5:30
  • This question says they get an error for the same thing. Commented Aug 20, 2020 at 5: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. Commented Aug 20, 2020 at 5:36
  • @ZanLynx But if the compiler supports VLA it should work without const as well. Commented Aug 20, 2020 at 18:40

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

I think const in the function signature prevents assigning the variable inside the function.
Yes, but to the compiler it doesn't make a difference whether it is const or not (for value types).
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.
I try it on compiler Compiler Explorer - link This compiled and work
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: stackoverflow.com/questions/39334435/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.