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?

3
  • 1
    And what exactly is this syntax error? Commented Jun 7, 2013 at 12:43
  • error C2059: syntax error : ')' on the line of push Commented Jun 7, 2013 at 12:43
  • It's for usual array, but maybe it will help: stackoverflow.com/questions/2236197/… Commented Jun 7, 2013 at 12:58

2 Answers 2

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.

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

11 Comments

@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: Clang refuses to compile when the extra braces are not there.
GCC also accepts the statement without the extra braces: see here.
@Fanael: That doesn't mean that GCC is wrong. Brace elision is a part of the C++11 standard.
@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.
|
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.

5 Comments

@CodeClown: Actually, I think it might be.
@CodeClown this is the right answer. The selected answer is wrong. I believe the extra braces can be elided in this case.
@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.
@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: Hmmm thinking about it you might be right. Upvoting your answer.

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.