Template parameter lists in lambda expressions is a C++20 feature.

(In fact, my GCC says that in the diagnostic: error: lambda templates are only available with -std=c++2a or -std=gnu++2a [-Wpedantic])

But you don't have to wait for C++20, it's already supported by GCC 8 with -std=c++2a flag.

And you'll have to change the call syntax: Instead of derived_factory<int>(), you need derived_factory.operator()<int>().


As an alternative (if you don't want a free function), I suggest using a variation of tag dispatch:

auto derived_factory = [](auto tag) {
    return new Derived<typename tag::type>();
};

template <typename T> struct tag_type {using type = T;};

// Usage:
derived_factory(tag_type<int>{})

Also, even if you make it compile somehow, this line:

auto derived = *(derived_factory<int>());

will cause a memory leak no matter what. To avoid that, you should store the result as a pointer or a reference. Or even better, use a smart pointer.

Answer from HolyBlackCat on Stack Overflow
Discussions

c++ - c++17: function template lambda specialization - Stack Overflow
Motivation: one has a function that accepts either a lambda or a value (for simplicity it can be either const char * or std::string) like following template void Func... More on stackoverflow.com
🌐 stackoverflow.com
c++ - Can lambda functions be templated? - Stack Overflow
In C++11, lambda functions can ... called C++14), this feature will be introduced. [Source] ... Note that though the syntax uses the keyword auto, the type deduction will not use the rules of auto type deduction, but instead use the rules of template argument deduction. Also see the proposal for generic lambda expressions(and the update to this). ... The rules of auto type deduction are specifically defined to be the same as those of template function argument deduction. 2016-04-17T19:54:30.6... More on stackoverflow.com
🌐 stackoverflow.com
c++ - What is the need of template lambda introduced in C++20 when C++14 already has generic lambda? - Stack Overflow
The new "familiar template syntax" for lambdas introduced in C++20 makes constructs such as for_types and for_range viable and way more readable compared to C++17 alternatives. More on stackoverflow.com
🌐 stackoverflow.com
[C++17] Question on assigning a function template lambda
Assuming MyLambda is just a misdirecting variable name, for initialization you can use MyFunction as initializer, and for assignment you can use it as right hand side of assignment. But it doesn't really make sense to ask about that, so what are you really asking? If you are assuming that std::function can store a template, no it cannot. More on reddit.com
🌐 r/cpp_questions
10
3
April 21, 2022
🌐
Open-std
open-std.org › jtc1 › sc22 › wg21 › docs › papers › 2017 › p0428r1.pdf pdf
Familiar template syntax for generic lambdas Document #: P0428R1 Date:
May 16, 2017 - parameter-declaration-clause and trailing-return-type respectively. For a generic lambda, the closure type has a public inline function call operator member template (17.5.2) whose template-parameter-list consists of the specified template-parameter-list, if any, to ·
🌐
MC++ BLOG
modernescpp.com › index.php › more-powerful-lambdas-with-c-20
More Powerful Lambdas with C++20 – MC++ BLOG
July 31, 2020 - To use it, I have to include the header <concepts>. I assume the small program is self-explanatory. There is one feature in the program templateLambdaVector.cpp, that you have probably missed. Since C++17, the compiler can deduce the class template type from its arguments (1).
🌐
Medium
ggulgulia.medium.com › c-20-lambda-expression-and-template-syntax-7f6ae2f9e7cd
C++20 lambda expression and template syntax | by Gajendra Gulgulia | Medium
July 26, 2021 - Since its addition to C++ in the ... and C++17 standards. Today they are one of the most ubiquitous feature. No doubt they are first class citizens of C++ and still continue to evolve. C++14 introduced the generic lambda expression into the core language feature which allowed the lambda expression to accept any parameter in its parameter list. C++20 has taken generic lambda expression to a next level whereby lambda expression allows template type parameter ...
🌐
Embarcadero
blogs.embarcadero.com › home › c++ › learn how to use c++ lambda expressions in c++17 with c++builder
Learn How To Use C++ Lambda Expressions In C++17 With C++Builder
November 10, 2020 - Currently, C++ is a completely ... how to use the Standard Template Library and the most important features of the C++17 that will greatly help you write readable, maintainable, and expressive code! One of the new features of C++11 was lambda expressions....
🌐
Open-std
open-std.org › jtc1 › sc22 › wg21 › docs › papers › 2017 › p0428r2.pdf pdf
Familiar template syntax for generic lambdas Document #: P0428R2 Date:
as normal function templates. The following details such areas where lambdas are lacking in their ... 2. It is often useful to retrieve the type of the parameter of a generic lambda, e.g. for accessing a · static member function or an alias nested inside it. However, retrieving such a type requires · using decltype, which includes its reference and cv qualifiers. This can often lead to unexpected ... The wording is based on the C++17 DIS [N4659]. At the very beginning of [expr.prim.lambda]
Find elsewhere
🌐
DEV Community
dev.to › fenbf › 2-lines-of-code-and-3-c-17-features-the-overload-pattern-pgg
2 Lines Of Code and 3 C++17 Features - The overload Pattern - DEV Community
July 26, 2019 - Custom template argument deduction rules - that allows converting a list of lambdas into a list of base classes for the overloaded class. Extension to aggregate Initialization - the overload pattern uses a constructor to initialise, but we don’t have to specify it in the class. Before C++17 it wasn’t possible.
🌐
Fluent C++
fluentcpp.com › home › the evolutions of lambdas in c++14, c++17 and c++20
The Evolutions of Lambdas in C++14, C++17 and C++20 - Fluent C++
December 24, 2021 - C++17 brought one major enhancement to lambdas: they can be declared constexpr: constexpr auto times2 = [] (int n) { return n * 2; }; Such lambdas can then be used in contexts evaluated at compile time: ... This is particularly useful in template programming.
🌐
C++ Stories
cppstories.com › 2019 › 03 › lambdas-story-part2
Lambdas: From C++11 to C++20, Part 2 - C++ Stories
March 11, 2019 - To recall, in C++17 a constexpr function has the following rules: ... a definition of a variable of non-literal type or of static or thread storage duration or for which no initialization is performed. ... template<typename Range, typename Func, typename T> constexpr T SimpleAccumulate(const Range& range, Func func, T init) { for (auto &&elem: range) { init += func(elem); } return init; } int main() { constexpr std::array arr{ 1, 2, 3 }; static_assert(SimpleAccumulate(arr, [](int i) { return i * i; }, 0) == 14); }
🌐
Daniel Lemire's blog
lemire.me › blog › 2025 › 03 › 15 › speeding-up-c-code-with-template-lambdas
Speeding up C++ code with template lambdas – Daniel Lemire's blog
March 16, 2025 - Unfortunately, it does not quite work. Given template lambda expressions, you cannot directly pass template parameters. In C++, lambdas are syntactic sugar for objects of an unnamed class (a closure type).
🌐
HackerNoon
hackernoon.com › all-about-lambda-functions-in-cfrom-c11-to-c17-2t1j32qw
All About Lambda Functions in C++: from C++11 to C++17 | HackerNoon
November 18, 2019 - Lambda function is quite an intuitive concept of Modern C++ introduced in C++11, So there are already tons of articles on lambda function tutorial over the internet. But still, there are some untold things(like IIFE, types of lambda, etc.) left, which nobody talks about.
🌐
Google Groups
groups.google.com › g › comp.lang.c++.moderated › c › PvTQg0788LM
Templated Lambda Functions?
You need some named object for reuse and in this case you want some kind of template. If you're attempting to use a bunch of local variables then you probably want a functor with state rather than a "free function with half-a-dozen parameters". ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... If you mean lambdas with templated function call operator: no, they don't exist.
🌐
Standard C++
isocpp.org › wiki › faq › cpp14-language
C++14 Language Extensions, C++ FAQ
Lambda function parameters can now be auto to let the compiler deduce the type. This generates a lambda type with a templated operator() so that the same lambda object can be invoked with any suitable type and a type-safe function with the right parameter type will be automatically generated.
🌐
Promit's Ventspace
ventspace.wordpress.com › 2022 › 04 › 11 › quick-snippet-c-type-trait-templates-for-lambda-details
Quick Snippet: C++ Type Trait Templates For Lambda Details – Promit's Ventspace
April 10, 2022 - #include <cstdio> #include <functional> #include <typeinfo> //just used for the demo, not needed by the templates //Tells us the first argument type in an args tuple template<typename T> struct first_arg { using type = std::tuple_element_t<0, T>; }; //In the case of an empty tuple, report the first type as void template<> struct first_arg<std::tuple<>> { using type = void; }; //These two use a member function pointer type to deduce types for a callable (lambdas, mainly) template<typename T> struct memfun_type { using type = void; }; template<typename Ret, typename Class, typename...