2

Not an actual problem but rather a fashion crisis..

 vector<array<unsigned int, 3>> tri;
 tri.push_back(array<unsigned int, 3> {0, 0, 0});

gives me a syntax error. Is there any way to initialize a std array with values into a vector in one line?

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Jake Freelander
  • 1,471
  • 1
  • 19
  • 26

2 Answers2

10

The first rule of std::array is: when in doubt, add more braces. That's because you're actually initializing the raw array subobject of the std::array.

tri.push_back(array<unsigned int, 3> {{0, 0, 0}});

Both GCC and Clang accept this statement.

  • @KarliRaudsepp I am not sure this answer is right. I think the extra braces can be elided here. In any case, as per the C++11 standard, it isn't crystal clear whether they are required or not (this might have been fixed in current drafts). – juanchopanza Jun 07 '13 at 13:02
  • @juanchopanza: Clang refuses to compile when the extra braces are not there. –  Jun 07 '13 at 13:03
  • 1
    GCC also accepts the statement without the extra braces: see [here](http://ideone.com/vHPLg7). – juanchopanza Jun 07 '13 at 13:03
  • @juanchopanza: yeah, but without them the code works only under GCC, with them under both GCC and Clang. Thus, safer to add them anyway, and I believe Clang may be onto something, as it's not the first time I see GCC accepting some code that is rejected by Clang. More often than not, such cases are seen as legitimate bugs by the GCC devs. –  Jun 07 '13 at 13:04
  • @juanchopanza: GCC emits a `warning: missing braces around initializer for ‘std::array::value_type [3] {aka unsigned int [3]}’ [-Wmissing-braces]`. So yeah it accepts it, but it still whines. – syam Jun 07 '13 at 13:05
  • 2
    @Fanael: That doesn't mean that GCC is wrong. Brace elision is a part of the C++11 standard. – Nicol Bolas Jun 07 '13 at 13:05
  • 3
    @Fanael the wording of the C++11 standard isn't completely clear about this. In fact it is plain confusing. They give an example implementation which uses a single plain array, but then they say it is only an example. There is no requirement that `std::array` be an aggregate containing a single aggregate. But as I said, maybe this has been clarified in favour of your solution. But then there is the issue of brace elision. I *think* it applies here. – juanchopanza Jun 07 '13 at 13:06
  • @syam but I am not completely convinced this warning is merited. There has been a lot of debate about this issue... – juanchopanza Jun 07 '13 at 13:08
  • @NicolBolas: note that doesn't mean that GCC is right, too. Let's just agree that the wording is a mess; and code that compiles under more than one compiler is better than one that compiles under only one. –  Jun 07 '13 at 13:46
  • @juanchopanza: so in the end I was right, in C++11 brace elision doesn't apply here (8.5.1/11). –  Jun 08 '13 at 11:43
  • @Fanael Good work! But are the braces required in the first place? I will re-read the relevant section of the standard. – juanchopanza Jun 08 '13 at 12:31
9

vs10 still wont accept it :/

And this is why it's important to always provide complete information in your questions.

Visual Studio 2010 does not implement uniform initialization (and that is uniform initialization, not merely aggregate initialization). It's not a C++11-compliant compiler; it just has a few C++11 features.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • @CodeClown: Actually, I think it might be. – John Dibling Jun 07 '13 at 12:56
  • @CodeClown this is the right answer. The selected answer is wrong. I believe the extra braces can be elided in this case. – juanchopanza Jun 07 '13 at 13:00
  • @John Dibling @ juanchopanza He just mentions that VS10 is not C++11 compliant. That should go into a comment. He is of course right that the original question is lacking crucial information like what compiler is used. A proper answer should explain what syntax is correct no matter what the compiler supports. – aggsol Jun 07 '13 at 13:03
  • 4
    @CodeClown: The question is "why doesn't this code work?" The answer is "Because you're using a compiler that doesn't accept it." – Nicol Bolas Jun 07 '13 at 13:04
  • @Nicol Bolas: Hmmm thinking about it you might be right. Upvoting your answer. – aggsol Jun 07 '13 at 13:07