可变参数聚合作为核心语言功能

Variadic aggregates as core language feature

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...}; }

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

有关相关想法,请参阅 N4235 Selecting from Parameter Packs

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

std::tuple 的定义非常复杂,为了使所有构造函数正确地模拟所有成员的属性(请参阅 N4064 了解该领域的最新变化)。

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

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

我想要一个功能,它可以更轻松地自动生成合适的构造函数来建模与从聚合初始化中免费获得的语义相同的语义。