3

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?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Test
  • 564
  • 3
  • 12

1 Answers1

6

This isn't a fold expression. You cannot have any expression statements in class scope. And as you point out, there are no parentheses that are part of the fold expression syntax.

This is a declaration with a parameter pack expansion. Pack expansions can be used in many more context than just fold expressions.

what will this statement do?

It will declare

using L::operator();

for each type L in the parameter pack Lambdas.

Usually using is used same as typedef

That's not the only use case of that keyword. In this context, it is used to introduce a member (function) of a base class into the derived class.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 2
    @HolyBlackCat Corrected. Parameter pack expansion in general was in C++11, but apparently using them in using declarations was introduced in C++17. – eerorika Aug 18 '21 at 13:53
  • can you please detail your response in context of my question? – Test Aug 18 '21 at 14:10
  • 2
    @Test The answer to your question `How will it fold?` is that it doesn't fold because it isn't a fold. The answer to your question `Where is the fold expression ()?` is that there isn't a fold expression in the example. – eerorika Aug 18 '21 at 14:11
  • what will this statement do? using Lambdas::operator()...; Usually using is used same as typedef but here it's usage isn't clear to me. – Test Aug 18 '21 at 14:37