6

std::tuple是高度模板加载的野兽。要访问第 n 个成员,编译器必须执行大量模板实例化,尽管它的性质很简单:访问相应虚构结构的第 n 个数据成员。似乎这std::tuple应该是一个核心语言功能,像这样(伪代码):

template< typename ...types >
struct/* or class, or even union */ V
{
    types... V; // defines implicitly `operator [/*constant expression*/]` to access by index
    // if more than one variadic parameter pack provided
    // (during expanding of parameter pack of template 
    // parameters in specializations) then instead of
    // `V` there can be specific data-member name (say, `x`),
    // but still with `x.operator []` implicitly defined

    // member functions and data members allowed
};

template< typename ...types >
V< std::decay_t< types >... > make_tuple(types &&... args)
{ return {std::forward< types >(args)...}; }
template< typename ...types >
V< types &&... > forward_as_tuple(types &&... args)
{ return {std::forward< types >(args)...}; }
template< typename ...types >
V< types &... > tie(types &... args)
{ return {args...}; }

是否有类似语言支持的可变参数数据成员定义语法之类的建议?

4

1 回答 1

9

有关相关想法,请参阅N4235 从参数包中选择。

这可能对 有用std::tuple,但我更感兴趣的是简化其构造的功能,而不是成员的选择(这相对简单)。

的定义std::tuple非常复杂,以使所有构造函数正确地对所有成员的属性进行建模(有关该区域的最新更改,请参见N4064 )。

当您使用聚合初始化时,编译器会自动检查聚合的每个成员是否可以从相应的初始化程序构造。它检查合适的构造函数,它们是否explicit接受相关的左值/右值类别等。

当您为 is 定义构造函数时std::tuple,必须非常复杂以确保仅发生有效转换,explicit构造函数在不应该使用时不使用等。

I'd like a feature that made it much easier to automatically generate suitable constructors to model the same semantics as you get for free from aggregate initialization.

于 2015-10-02T12:25:35.877 回答