jawbreaker.io

Unlocking the Power of Variadic Class Template Arguments in C++


Listen Later

Hello and welcome to C++ Insights. Today we’re diving into Sandor Dargo’s article on variadic class template arguments. As he puts it, “Variadic templates don’t accept a fixed size of parameters, but anything from one to more,” and that flexible set is known as the parameter pack.

First, you can expand your pack in a constructor or member function. For example, a Storage class uses a std::tuple to hold elements of any types passed in. You might even need a simple deduction guide so you don’t have to list each type explicitly.

Next, you can expand at the inheritance list. Dargo shows a Visitor struct inheriting from multiple lambdas, using “using Ts::operator()…” to bring each callable into scope. Thanks to Class Template Argument Deduction, you can “inherit from lambdas without saving their generated types anywhere” and build a slick visitor in just a few lines.

And coming in C++26, you can even expand variadic friends: “friend Ts...;” makes it easy to grant access to any number of friend classes.

That’s a quick tour of variadic class template arguments—see you next time on C++ Insights!
Link to Article

...more
View all episodesView all episodes
Download on the App Store

jawbreaker.ioBy jawbreaker.io