嵌套std :: array时,结构初始化程序中的多余元素 [英] Excess elements in struct initializer when nested std::array

查看:79
本文介绍了嵌套std :: array时,结构初始化程序中的多余元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下行的 return 行上得到结构初始化程序中的个多余的元素:

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
    }
}

我希望花括号字面量能以嵌套的方式工作,因为如果我只用一个std :: array而不是嵌套的话,这样做会很好。

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?

注意,建议的重复方法实际上并没有解决std :: array的奇怪行为

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

推荐答案

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");
    }
}

在线演示

您的代码有两个问题:


  1. 您缺少用于内部数组的括号。

  2. 如@Praetorian所述, colorBlend 没有默认情况下的返回值。

  1. You were lacking braces for the inner arrays.
  2. As noted by @Praetorian, colorBlend had no return value for the default case.

这篇关于嵌套std :: array时,结构初始化程序中的多余元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆