C++ 模板参数演绎的奇怪行为
下面的代码可以在c++17下编译C++ 模板参数演绎的奇怪行为,c++,c++17,C++,C++17,下面的代码可以在c++17下编译 template<class... Ts> struct Test : Ts... { using Ts::operator()...; }; template<class... Ts> Test(Ts...) -> Test<Ts...>; int main() { Test test { [](const int& i) { }, [](con
template<class... Ts>
struct Test : Ts...
{
using Ts::operator()...;
};
template<class... Ts> Test(Ts...) -> Test<Ts...>;
int main() {
Test test
{
[](const int& i) { },
[](const float& f) { }
};
}
它不会编译,因为测试没有这样的构造函数,它有两个参数。我想知道为什么原始代码可以工作?因为聚合初始化在C++17中变得更奇怪。基本上,如果使用一个或多个公共基类聚合初始化一个类,则初始化器列表的第一个元素用于初始化基类。在这种情况下,在模板参数推导之后,可以使用其默认的复制构造函数从lambda参数正确初始化基类。@cppleerner Test没有数据成员,聚合初始化在这里是如何工作的?
Test test
( //{ is changed to (
[](const int& i) { },
[](const float& f) { }
);//} is changed to )