C++ Weekly - Ep 48 - C++17's Variadic `using`

C++ Weekly With Jason Turner · 15K views · 2017 · 5:16

Open on YouTube

C, C++, Rust, Go & systems languagesProgramming language designtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when they are following or debugging its Merged/overload-set example, and when pointing to a short explanation of the technique for use with std::visit.

Recommended by 1 people across 1 different months, 2017 to 2020.

What it is

A five-minute episode of Jason Turner's C++ Weekly showing how C++17 allows `using` with a parameter pack, so a struct can inherit from several lambdas and pull in all their call operators. It builds an overload-set helper using variadic inheritance, a perfect-forwarding constructor and a deduction guide.

What people said

Recommends Daniel H · 2017-07 · Stack Overflow · on “Using std::visit with variadic template struct”

There is a proposal [LINK] to add something like that to the standard library as `std::overload`, and a 5-minute video [this video] about implementing this.

Cites HanXu · 2017-04 · Stack Overflow · on “Deduction guide for variadic template constructor fails”

I am trying to reproduce the result from video C++ Weekly - Ep 48 - C++17's Variadic using [this video] , but failed. The problem can be simplified to the following code piece. Suppose I have such generic structure: template <class... T> struct Container { template <class... U> Container(U... us) {} }; Now I can initialize a Container with any arguments, like auto d = Container(1,2,3); However, the compiler will never know what type the d is. To resolve this, we should provide a deduction guide, e.g. template <class... U> Container(U...) -> Container<double, int, bool> According to the video, the compiler now should know d has type Container<double, int, bool> . However, the code does not work as

Cites Kobi · 2017-05 · Stack Overflow · on “Variadic class template and variadic constructor”

On C++ weekly ep. 48 by Jason Turner [this video] there is the following code: template<typename ... B> struct Merged : B... { template <typename ... T> Merged(T && ... t) : B(std::forward<T>(t))... { } using B::operator()...; }; It fails with the following on gcc 7.1: error: mismatched argument pack lengths while expanding 'B' What is the proper way to Expand B? (BTW, in the above link, the code seems to compile with some 7.0 snapshot). Edit1: As noted by @Jarod42, it will compile with Structs acting as the functors. The actual video use lambda and it seems to break there. auto l1 = [] { return 4 ; }; auto l2 = [](const int i) { return i * 10; }; // This would work, S1, S2 are just functors stru

Cites HuskyDucky · 2020-03 · Stack Overflow · on “Variadic template error: 'In instantiation of' (gcc 9.2)”

I'm learning about variadic template on c++17 on the Jason Turner's [this video] then I copy his example code (shown below). In his video he uses the site godbolt.org [LINK] with the gcc 7. #include <utility> template<typename ... B> struct Merged : B ... { template<typename ... T> Merged(T&& ... t) : B(std::forward<T>(t))... {} using B::operator()...; }; template<typename ... T> Merged(T...) -> Merged<std::decay_t<T>...>; int main() { const auto l1 = []() { return 4; }; const auto l2 = [](const int i) { return i * 10; }; Merged merged(l1, l2, [](const double d) { return d * 3.2; }); return 0; } I tried the code in my computer with the gcc 9.2 and I got the follow error: In instantiation of 'Merg