3

On the following code

std::array<int,3> myarray = {10,20,30};

I am receiving the following compiler warning

warning: missing braces around initializer for ‘std::array<int, 3u>::value_type [3] {aka int [3]}’ [-Wmissing-braces]

Why ?

toolchain: (edit)

g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1
  • Should we guess what toolchain you're using? Commented Feb 10, 2013 at 3:48

2 Answers 2

6

Try this:

std::array<int,3> = {{10, 20, 30}}

I think this was a bug they fixed in versions > 4.6

Sign up to request clarification or add additional context in comments.

Comments

6

As Tyler indicated, std::array is a POD, so it has no constructors, and it contains an array. To initialise it with brace syntax, you initialise the variable, and then the array inside the variable, with the nested braces.

{ { 10, 20, 30 } }
  ^ For the array member variable inside the std::array object
^ For the std::array object

Actually this is a bug in your compiler, because aggregate initialisation allows you to remove a layer of braces after an =. So these two are legal:

std::array<int,3> x = {10, 20, 30};
std::array<int,3> y  {{10, 20, 30}};

But not

std::array<int,3> z {10, 20, 30};

The last one compiles on GCC but it's a nonstandard extension and you should get a warning.

9 Comments

Works with only a single pair of braces on g++ 4.7, though.
Works single-bracket as well on my mac, LLVM 4.2. Thus the reason I asked what chain he had.
@WhozCraig apparently aggregate initialisation allows you to remove a layer after an =.
@SethCarnegie yeah, I see that now. Interesting how you're need to be container-aware without it. At least you can still do it though, which is more than many of things in older implementations.
@Seth "But not std::array<int,3> z {10, 20, 30}; " <- that works on g++ 4.7.
|

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.