我想知道元组是否可以通过初始化列表初始化(更准确地说 - 初始化列表的initializer_list)?考虑元组定义:
typedef std::tuple< std::array<short, 3>,
std::array<float, 2>,
std::array<unsigned char, 4>,
std::array<unsigned char, 4> > vertex;
Run Code Online (Sandbox Code Playgroud)
有没有办法做到以下几点:
static vertex const nullvertex = { {{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}} };
Run Code Online (Sandbox Code Playgroud)
我只想实现使用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}}
};
Run Code Online (Sandbox Code Playgroud)
没有理由我必须使用元组,只是想知道.我问,因为我无法通过我尝试进行这种元组初始化而生成的g ++模板错误.
@Motti:所以我错过了统一初始化的正确语法 - …
我今天发现了一段有趣的代码:
auto ch = (double(), (float(), int()))["\t\a\r\n\0"]["abcdefghij"];
Run Code Online (Sandbox Code Playgroud)
其作用相同:
char str[] = "abcdefghij";
char ch = str['\t'];
Run Code Online (Sandbox Code Playgroud)
为什么它甚至可能?特别是为什么编译器从字符串中挑选第一个char并将其用作下标而不是抛出错误?
我想知道调用模板方法的正确语法是什么:
struct print_ch {
print_ch(char const& ch) : m_ch(ch) { }
~print_ch() { }
template<typename T>
void operator()() {
std::cout << static_cast<T>(m_ch) << std::endl;
}
private:
char m_ch;
};
Run Code Online (Sandbox Code Playgroud)
我想出了这样的事:
print_ch printer('c');
printer.operator()<int>();
Run Code Online (Sandbox Code Playgroud)
它似乎工作(GCC 4.5),但当我在另一个模板化方法中使用它时,例如:
struct printer {
typedef int print_type;
template<typename T_functor>
static void print(T_functor& fnct) {
fnct.operator()<print_type>();
}
};
Run Code Online (Sandbox Code Playgroud)
编译失败error: expected primary-expression before '>' token.任何想法让它正确吗?提前致谢.