Variadic class template and inheritance - built default compiler constructor

How is it that the code below only works with the default constructor compiler? I would expect this for a POD, but the structure below is probably not a POD, so this should be something else.

template <typename ... T> struct C : T ... { using T::operator()...; }; // template class guidance feature of C++17 template <typename ... T> C(T...) -> C<T...>; int main(){ C c { []{}, [](int){} }; c(3); } 

This question arises as a continuation of the Jason Turner C ++ gospel weekly of the weekly ep 49/50, where he defined a constructor of variables with std::forward<T>(t)...

+5
source share
1 answer

There are no designers. This code works due to the merging of three functions new in C ++ 17:

  • Conclusion of a template parameter for designers ( P0091 ).
  • Implement Initialization Extension ( P0017 )
  • Modernization of use of declarations ( P0195 ).

What happens on this line:

 C c { []{}, [](int){} }; 

is that we first use the output of template parameter (1) to infer that c really has type C<__lambda1, __lambda2> . This is done using a deduction guide.

Further, since C<__lambda1, __lambda2> is a collection (due to (2) relaxation of the restrictions of the base class - you are correct that you are not considered an aggregate in C ++ 11/14), we can use the initialization of the aggregate to initialize This. We do not use a constructor. The way aggregate initialization now works with base classes is that we just need to initialize the bases from left to right. So, the first expression ( []{} ) is used to initialize the first base class ( __lambda1 ), and the second expression ( [](int){} ) is used to initialize the second base class ( __lambda2 ).

Finally, calling c(3) works because (3) allowed you to simply write

 using T::operator()...; 

which includes lambdas call statements in area c , where overload resolution might work as expected. As a result, we call the __lambda2 call __lambda2 , which does nothing.

+7
source

All Articles