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...}; }
是否有类似语言支持的可变参数数据成员定义语法之类的建议?