Variadic聚合作为核心语言特征

Ori*_*ent 6 c++ tuples variadic-templates c++11 c++17

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...}; }
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*ely 9

有关相关提示,请参阅N4235从参数包中选择.

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

定义std::tuple非常复杂,以便使所有构造函数正确地模拟所有成员的属性(有关该区域中的最新更改,请参阅N4064).

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

std::tupleis 定义构造函数时,必须非常复杂,以确保只发生有效的转换,explicit当它们不应该等时,不使用那些构造函数.

我想要一个功能,它可以更容易地自动生成合适的构造函数来模拟相同的语义,因为你可以从聚合初始化中免费获得.