Whereisthefoldexpression()?
Reference fluentCPP article
The below code explanation says that this structure inherits from several lambdas, can be constructed from those lambdas, and folds over the using expression.
template<typename... Lambdas>
struct overloaded : public Lambdas...
{
explicit overloaded(Lambdas... lambdas) : Lambdas(lambdas)... {}
using Lambdas::operator()...;
};
My doubt is that parentheses i.e () indicate a c++17 fold expression but I don't see any enclosing parentheses around the using statement. How will it fold?
回答
这不是折叠表达式。类范围内不能有任何表达式语句。正如您所指出的,折叠表达式语法中没有括号。
这是一个带有参数包扩展的声明。包扩展可以在更多的上下文中使用,而不仅仅是折叠表达式。
这个声明会做什么?
它会声明
using L::operator();
对于L参数包中的每种类型Lambdas。
通常使用与 typedef 相同
这不是该关键字的唯一用例。在此上下文中,它用于将基类的成员(函数)引入派生类。
- @HolyBlackCat 已更正。参数包扩展一般是在 C++11 中,但显然在 C++17 中引入了在 using 声明中使用它们。
- @Test 您的问题“它将如何折叠?”的答案是它不会折叠,因为它不是折叠。您的问题“折叠表达式 () 在哪里?”的答案是示例中没有折叠表达式。