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 OverflowTemplate 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.
Waiting for C++20, you can return the lambda from a template class
template <typename ResourceType>
auto make_derived_factory ()
{ return []{ return new Derived<ResourceType>{}; }; }
auto derived = make_derived_factory<int>();
int main ()
{
auto df { derived() };
}
Suppose some template class template<typename T> class MyClass using which some function template<typename T> MyClass<T> MyFunction(MyClass<T>) is defined.
How would a function template object std::function<MyClass<T>(MyClass<T>)> MyLambda be assigned to MyFunction?
Edit[0]: skeleton code
Edit[1]: use case
c++ - c++17: function template lambda specialization - Stack Overflow
c++ - Can lambda functions be templated? - Stack Overflow
c++ - What is the need of template lambda introduced in C++20 when C++14 already has generic lambda? - Stack Overflow
[C++17] Question on assigning a function template lambda
UPDATE 2018: C++20 will come with templated and conceptualized lambdas. The feature has already been integrated into the standard draft.
UPDATE 2014: C++14 has been released this year and now provides Polymorphic lambdas with the same syntax as in this example. Some major compilers already implement it.
At it stands (in C++11), sadly no. Polymorphic lambdas would be excellent in terms of flexibility and power.
The original reason they ended up being monomorphic was because of concepts. Concepts made this code situation difficult:
template <Constraint T>
void foo(T x)
{
auto bar = [](auto x){}; // imaginary syntax
}
In a constrained template you can only call other constrained templates. (Otherwise the constraints couldn't be checked.) Can foo invoke bar(x)? What constraints does the lambda have (the parameter for it is just a template, after all)?
Concepts weren't ready to tackle this sort of thing; it'd require more stuff like late_check (where the concept wasn't checked until invoked) and stuff. Simpler was just to drop it all and stick to monomorphic lambdas.
However, with the removal of concepts from C++0x, polymorphic lambdas become a simple proposition again. However, I can't find any proposals for it. :(
In C++20 this is possible using the following syntax:
auto lambda = []<typename T>(T t){
// do something
};
C++14 generic lambdas are a very cool way to generate a functor with an operator () that looks like this:
template <class T, class U>
auto operator()(T t, U u) const;
But not like this:
template <class T>
auto operator()(T t1, T t2) const; // Same type please
Nor like this:
template <class T, std::size_t N>
auto operator()(std::array<T, N> const &) const; // Only `std::array` please
Nor like this (although this gets a bit tricky to actually use):
template <class T>
auto operator()() const; // No deduction
C++14 lambdas are fine, but C++20 allows us to implement these cases without hassle.
Since you can use templated lambdas in C++20, you can restrict your types in an easier way than an SFINAE expression:
auto lambda = []<typename T>(std::vector<T> t){};
This lambda will work only with vector types.