从初始化器列表初始化std :: tuple [英] Initializing std::tuple from initializer list

查看:583
本文介绍了从初始化器列表初始化std :: tuple的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道元组是否可以通过初始化列表初始化(更确切地说,可以通过initializer_lists的initializer_list初始化)?考虑元组的定义:

I'm wondering whether the tuple can be initialized by initializer list (to be more precise - by initializer_list of initializer_lists)? Considering the tuple definition:

typedef std::tuple< std::array<short, 3>,
                    std::array<float, 2>,
                    std::array<unsigned char, 4>,
                    std::array<unsigned char, 4> > vertex;

是否可以执行以下操作:

is there any way of doing the following:

static vertex const nullvertex = { {{0, 0, 0}},
                                   {{0.0, 0.0}},
                                   {{0, 0, 0, 0}},
                                   {{0, 0, 0, 0}} };

我只想实现与使用struct而不是tuple相同的功能(因此,仅通过初始化数组initializer_list):

I just want to achieve same functionality I got using struct instead of tuple (thus only arrays are initialized by 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 ++模板错误。

There is no reason I must use tuples, just wondering. I'm asking, because I'm unable to go through g++ templates errors which are generated by my attempt of such tuple initialization.

@Motti:所以我错过了正确的方法统一初始化的语法-

@Motti: So I missed the proper syntax for uniform initialization -

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并用合适的构造函数似乎并非易事。

But it seems that all the trouble lies in arrays, which got no constructor for initializer_list and wrapping arrays with proper constructor seems not so easy task.

推荐答案

初始化列表与元组无关。

Initializer lists aren't relevant for tuples.

我认为您在C ++ 0x中混淆了花括号的两种不同用法。

I think that you're confusing two different uses of curly braces in C++0x.


  1. initializer_list< T> 是同类集合(所有成员必须为同一类型,因此与 std不相关: :tuple

  2. 统一初始化是使用大括号构造各种对象的地方;数组,POD和带有构造函数的类。这还具有解决最烦人的解析)的好处。

  1. initializer_list<T> is a homogeneous collection (all members must be of the same type, so not relevant for std::tuple)
  2. Uniform initialization is where curly brackets are used in order to construct all kinds of objects; arrays, PODs and classes with constructors. Which also has the benefit of solving the most vexing parse)

以下是简化版本:

std::tuple<int, char> t = { 1, '1' }; 
// error: converting to 'std::tuple<int, char>' from initializer list would use
// explicit constructor 'std::tuple<_T1, _T2>::tuple(_U1&&, _U2&&) 
// [with _U1 = int, _U2 = char, _T1 = int, _T2 = char]'

std::tuple<int, char> t { 1, '1' }; // note no assignment
// OK, but not an initializer list, uniform initialization

错误消息表明,您正在尝试隐式调用构造函数,但它是显式构造函数,因此您不能这样做。

The error message says is that you're trying to implicitly call the constructor but it's an explicit constructor so you can't.

基本上,您要尝试的是

struct A { 
    explicit A(int) {}
};

A a0 = 3;
// Error: conversion from 'int' to non-scalar type 'A' requested

A a1 = {3}; 
// Error: converting to 'const A' from initializer list would use 
// explicit constructor 'A::A(int)'

A a2(3); // OK C++98 style
A a3{3}; // OK C++0x Uniform initialization

这篇关于从初始化器列表初始化std :: tuple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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