理论上是否可以添加语言功能以将结构解压缩到函数实际参数列表中?我的意思是以下。
template< typename ...Ts >
void f(Ts... values) { (std::cout << ... << values) << std::endl; }
struct S { int a; char c; double d; };
S s{1, '2', 3.0};
f([s]);
void g(int, int, int) {}
g([s]); // warning about narrowing conversion
void h(int &, int &, int &) {}
h([s]); // hard error ("cannot bind to...")
处理成员数量未知的合适结构会很方便。由于缺少模板上下文,当前的结构化绑定无法“解包”未知数量的组件(如auto... [x] = s;
)的结构,因此只能operator ...
处理可变数量的类型/值。
这种方式有什么缺点?