C++11 “overloaded lambda” with variadic template and variable capture

Overload resolution works only for functions that exist in a common scope. This means that the second implementation fails to find the second overload because you don’t import function call operators from overload<Frest…> into overload<F0, Frest…>. However, a non-capturing lambda type defines a conversion operator to a function pointer with the same signature as the … Read more

Is there a name for this tuple-creation idiom?

I think this is a subtle implementation of a Monad-like thing, specifically something in the same spirit of the continuation monad. Monads are a functional programming construction used to simulate state between different steps of a computation (Remember that a functional language is stateless). What a monad does is to chain different functions, creating a … Read more

Variadic deduction guide not taken by g++, taken by clang++ – who is correct?

This is gcc bug 80871. The issue is, we end up with this set of candidates for deduction: template <class… Types, class… Args> list<Types…> __f(Args… ); // constructor template <class… Args> list<Args…> __f(Args… ); // deduction-guide Both are valid (Types… can deduce as empty in the first case), but the call here should be ambiguous … Read more

generic member function pointer as a template parameter

You could try something like this: template <typename T, typename R, typename …Args> R proxycall(T & obj, R (T::*mf)(Args…), Args &&… args) { return (obj.*mf)(std::forward<Args>(args)…); } Usage: proxycall(obj, &hello::f); Alternatively, to make the PTMF into a template argument, try specialization: template <typename T, T> struct proxy; template <typename T, typename R, typename …Args, R (T::*mf)(Args…)> … Read more

Variadic templates

One of the simplest possible examples is the following implementation of max which isn’t even templated on types. int maximum(int n) { return n; } template<typename… Args> int maximum(int n, Args… args) { return max(n, maximum(args…)); } Only slightly more complex is the canonical printf implementation: void printf(const char *s) { while (*s) { if … Read more

How can I expand call to variadic template base classes?

Sure. You need a context that permits pack expansion – a simple one is a braced initializer list, which also has the benefit of guaranteeing left-to-right evaluation: using expander = int[]; (void) expander { 0, ((void) As::id(), 0)… }; … expands a pattern to its left; in this case the pattern is the expression ((void) … Read more

c++ lambdas how to capture variadic parameter pack from the upper scope

Perfect capture in C++20 template <typename … Args> auto f(Args&& … args){ return [… args = std::forward<Args>(args)]{ // use args }; } C++17 and C++14 workaround In C++17 we can use a workaround with tuples: template <typename … Args> auto f(Args&& … args){ return [args = std::make_tuple(std::forward<Args>(args) …)]()mutable{ return std::apply([](auto&& … args){ // use args … Read more