6

I'm getting Excess elements in struct initializer on the return line of the following:

using triangleColor = std::array<std::array<float, 4>, 3>;

triangleColor colorBlend(TriangleColorBlend c){
    switch (c) {
        case TriangleColorBlend::white:
            return {{1.0,1.0,1.0,1.0},{0.7,0.7,0.7,1.0},{0.5,0.5,0.5,1.0}};
            break;

        ... // other cases
    }
}

I was hoping the curly-brace literal would work in the nested fashion, as it works fine if I do this with just a single std::array, not nested.

Is the above simply not possible, and why not?

Note, the suggested duplicate doesn't really address the odd behavior of std::array in a nested situation.

2
  • 1
    Please take any discussion about the appropriateness of this question to the meta question, but the OP is right: a question asking about how to do something does not necessarily require an MCVE for it to stay open. I'll say more in meta. Shog9 talked about this in this meta post. Commented Jan 26, 2016 at 20:08
  • 1
    @GeorgeStocker Retracted now. You're right I've been arguing about that earlier, and been proved wrong. Commented Jan 26, 2016 at 20:16

3 Answers 3

7
triangleColor colorBlend(TriangleColorBlend c) {
    switch (c) {
    case TriangleColorBlend::white:
        return {{
            {{ 1.0f, 1.0f, 1.0f, 1.0f }},
            {{ 0.7f, 0.7f, 0.7f, 1.0f }},
            {{ 0.5f, 0.5f, 0.5f, 1.0f }}
        }};
    default:
        throw std::invalid_argument("c");
    }
}

Online Demo

There were two issues with your code:

  1. You were lacking braces for the inner arrays.
  2. As noted by @Praetorian, colorBlend had no return value for the default case.
Sign up to request clarification or add additional context in comments.

5 Comments

This seems like a work around. The important question is why does the OP's code not work?
@RSahu : This is not a workaround, it is the corrected code.
This indeed solved the problem, many thanks. I was missing the extra braces. I thought in C++14 they were no longer necessary, but I might have misunderstood that detail.s Thanks!
@johnbakers : In this case they can be elided for the inner arrays (demo), but the outer array requires both.
@ildjarn, it seemed to me to be a workaround since there was no explanation of whey the OP's code didn't work. I understand now why it isn't a work around.
3

You are missing a set of brackets.

return {{1.0,1.0,1.0,1.0},{0.7,0.7,0.7,1.0},{0.5,0.5,0.5,1.0}};

Should be

return {{{1.0,1.0,1.0,1.0},{0.7,0.7,0.7,1.0},{0.5,0.5,0.5,1.0}}};

You can see it working in this minimal example

Comments

3

Another work around:

triangleColor colorBlend(TriangleColorBlend c){
   using t1 = std::array<float, 4>;
   switch (c) {
      case TriangleColorBlend::white:
         return {t1{1.0,1.0,1.0,1.0},t1{0.7,0.7,0.7,1.0},t1{0.5,0.5,0.5,1.0}};
         break;

      default:
         break;
   }
   return triangleColor{};
}

The answer to the question of why

        return {{1.0,1.0,1.0,1.0},{0.7,0.7,0.7,1.0},{0.5,0.5,0.5,1.0}};

does not work can be found at https://stackoverflow.com/a/8192275/434551:

std::array is an aggregate by the rules of C++11, and therefore it can be created by aggregate initialization. To aggregate initialize the array inside the struct, you need a second set of curly braces.

Comments

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.