Questions tagged [generic-lambda]

Generic Lambda lets Lambda function parameters be set to 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.

Generic Lambda lets Lambda function parameters be set to 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.

More Info : https://en.wikipedia.org/wiki/C%2B%2B14#Generic_lambdas

& https://isocpp.org/wiki/faq/cpp14-language#generic-lambdas

114 questions
11
votes
5 answers

Arity of a generic lambda

It is possible to deduce arity of a non-generic lambda by accessing its operator(). template struct fInfo : fInfo { }; template struct…
ScarletAmaranth
  • 5,065
  • 2
  • 23
  • 34
10
votes
3 answers

Is it possible to static_assert that a lambda is not generic?

I implemented a Visit function (on a variant) that checks that the currently active type in the variant matches the function signature (more precisely the first argument). Based on this nice answer. For example #include #include…
9
votes
3 answers

C++14: Generic lambda with generic std::function as class member

Consider this pseudo-snippet: class SomeClass { public: SomeClass() { if(true) { fooCall = [](auto a){ cout << a.sayHello(); }; } else { fooCall = [](auto b){ cout <<…
Juergen
  • 3,489
  • 6
  • 35
  • 59
9
votes
1 answer

Generic lambdas with statically sized arrays as arguments

Is the following generic (polymorphic) lambda legal C++14? auto f = [](auto x[3]) { x[0]; x[1]; // etc. }; GCC and Clang 4 accept the code, but Visual Studio 2017 does not. Is it legal? error C3318: 'auto [3]': an array cannot have an…
Petter
  • 37,121
  • 7
  • 47
  • 62
9
votes
3 answers

Using SFINAE with generic lambdas

Can generic lambdas take advantage of the "Substitution Failure Is Not An Error" rule ? Example auto gL = [](auto&& func, auto&& param1, auto&&... params) -> enable_if_t< is_integral< std::decay_t
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
8
votes
2 answers

limits and uses of C++20 template lambas

A couple of related questions for C++ standard gurus. The incoming C++20 introduces template lambdas (P0428R2). So instead of auto x = [](auto x, auto y){ return x+y; }; we can specify the template parameter as follows auto x = [](T x,…
max66
  • 65,235
  • 10
  • 71
  • 111
8
votes
4 answers

How to change the last argument in the parameter pack?

I have a function f1() template void f1(Args... args) { // the implementation is just an example, I don't really need a complicated // way to sum numbers boost::fusion::vector v(args...); std::cout <<…
facetus
  • 1,091
  • 6
  • 20
8
votes
2 answers

std::equal_range with lambda

Lets say I have a vector of strings and I want to find all strings, that start with 'a', so I can do this: struct cmp { bool operator()( const std::string &s, char c ) const { return s.front() < c; } bool operator()( char c, const…
Slava
  • 43,454
  • 1
  • 47
  • 90
8
votes
1 answer

When use a function template instead of a generic lambda?

I can write a function template: template void f1(T parameter) { ... } But in C++14, I can also create a generic lambda: auto f2 = [](auto parameter) { ... }; Within f1 I can refer to T directly. Within f2, there's no T to refer to,…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
7
votes
3 answers

generating calls to lambdas with arbitrary number of parameters

The following definition has proven to be very useful for me: template void apply_on_each_args(Func f, Args... args) { (f(args), ...); } Basically, the arguments pack folded on comma operator, allows to define several…
7
votes
3 answers

How to pass first N args to a C++ function

I've got a function like this: void loadData(std::function callback) { // data loading stuff callback(body, subject, header); } The problem is I'm not necessarily need to use subject and header…
Denis Sheremet
  • 2,453
  • 2
  • 18
  • 34
7
votes
1 answer

Should non-capturing generic lambdas decay to function pointers?

Consider the following code: int main() { auto l = [](auto){}; void(*p)(int) = l; } It works just fine both with GCC and clang. Let's consider the following slightly modified version: int main() { auto l = [](auto...){}; …
skypjack
  • 49,335
  • 19
  • 95
  • 187
6
votes
2 answers

return type deduction of recursive function

Recently, I read Barry's answer to this question Recursive lambda functions in C++11: template struct y_combinator { F f; // the lambda will be stored here // a forwarding operator(): template
6
votes
1 answer

Clang claims constexpr member of generic lambda argument is not constexpr

I would like to write a generic lambda as a visitor for a variant. The members of this variant contain a constexpr member value, which I would like to use in the visitor. For example: #include template struct S { constexpr…
Claudius
  • 550
  • 3
  • 14
6
votes
2 answers

Generic std::function to store a generic lambda

When generic lambda is stored as a std::function, we need to provide a concrete type, something like, std::function thus binding to a specific type, The following declaration: std::function throws a compiler error. I…
Rajesh
  • 356
  • 1
  • 5
  • 15