1

This answer says that C++11 has new feature which allows us to initialize "variable-sized arrays" like this:

auto array = new double[M][N]();

But when I try to do this, I get following error:

array size in new-expression must be constant

I didn't forget to add -std=c++0x in the .pro file. I tested another C++11 features and it works just fine.

Was he wrong? Or am I?

Thanks.

Community
  • 1
  • 1
pushandpop
  • 455
  • 1
  • 10
  • 22
  • 2
    In the answer you cite, `M` and `N` are in fact constants. The answer is about that pair of parens at the end which causes the array to be zero-initialized (which I don't believe is new in C++11, either). – Igor Tandetnik Jun 28 '15 at 16:09
  • You declared `const auto M, N`? – mazhar islam Jun 28 '15 at 16:09
  • Read the answers to the post you linked carefully. Also, this hasn't changed between C++03 and C++11. – juanchopanza Jun 28 '15 at 16:10
  • 2
    What is this question about? Firstly, C++ does not have variable-sized arrays. A feature vaguely similar to C VLA exists in modern C++, but it is much more restrictive. You can't create multi-dimensional variable-sized arrays in C++. Secondly, the *initialization* syntax you used dates back to the very beginning - C++98 - and has nothing to do with C++11. Thirdly, the only C++11 thing you have there is `auto`, but it has nothing to do with *initialization*. So, what is this question about exactly? – AnT stands with Russia Jun 28 '15 at 16:23

1 Answers1

2

C++ does not have such unrestricted run-time sized arrays. C++11 introduced a feature remotely similar to C VLAs, but it is significantly more limited. You are not allowed to have VLA of VLAs in C++, meaning that the second, third and further dimensions of any multi-dimensional array in C++ must be constant expressions.

Apparently, this is the requirement you violated. Your N is not a constant expression.

In any case, the title of your question talks about initialization, while in reality the problem you experienced is not related to initialization at all. The initialization syntax you used - () - is not new for C++11, it existed in C++ since the very first language standard.

The only C++11 feature in your code is the above usage of auto. But it has nothing to do with initialization or arrays specifically.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • I've mistakenly missed ``const`` keyword, bc of the beginning of the answer. I'm sorry for that, you're right. I will use 2D vector. – pushandpop Jun 29 '15 at 03:48