【发布时间】:2010-08-05 08:43:11
【问题描述】:
我想知道元组是否可以通过初始化列表初始化(更准确地说,通过初始化列表的初始化列表)?考虑元组定义:
typedef std::tuple< std::array<short, 3>,
std::array<float, 2>,
std::array<unsigned char, 4>,
std::array<unsigned char, 4> > vertex;
有没有办法做到以下几点:
static vertex const nullvertex = { {{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}} };
我只想实现与使用 struct 而不是 tuple 相同的功能(因此只有数组由 initializer_list 初始化):
static struct vertex {
std::array<short, 3> m_vertex_coords;
std::array<float, 2> m_texture_coords;
std::array<unsigned char, 4> m_color_1;
std::array<unsigned char, 4> m_color_2;
} const nullvertex = {
{{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}}
};
我没有理由必须使用元组,只是想知道。我在问,因为我无法通过我尝试进行此类元组初始化而生成的 g++ 模板错误。
@Motti:所以我错过了统一初始化的正确语法 -
static vertex const nullvertex = vertex{ {{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}} };
和
static vertex const nullvertex{ {{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}} };
但似乎所有的麻烦都在于数组,它没有用于 initializer_list 的构造函数,并且用适当的构造函数包装数组似乎不是一件容易的事。
【问题讨论】: