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
110
votes
4 answers

What is the need of template lambda introduced in C++20 when C++14 already has generic lambda?

c++14 introduced generic lambdas that made it possible to write following: auto func = [](auto a, auto b){ return a + b; }; auto Foo = func(2, 5); auto Bar = func("hello", "world"); It is very clear that this generic lambda func works just like…
coder3101
  • 3,920
  • 3
  • 24
  • 28
65
votes
3 answers

Is there a name for this tuple-creation idiom?

On the Boost mailinglist, the following clever trick to create a tuple-like entity was recently posted by @LouisDionne: #include auto list = [](auto ...xs) { return [=](auto access) { return access(xs...); }; }; auto length =…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
57
votes
3 answers

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

I study the generic lambdas, and slightly modified the example, so my lambda should capture the upper lambda's variadic parameter pack. So basically what is given to upper lambda as (auto&&...) - should be somehow captured in [=] block. (The perfect…
barney
  • 2,172
  • 1
  • 16
  • 25
38
votes
2 answers

Confusing templates in C++17 example of std::visit

When looking at std::visit() page in cppreference, https://en.cppreference.com/w/cpp/utility/variant/visit, I encountered the code I can't make sense of... Here's the abbreviated version: #include #include #include…
Boris
  • 748
  • 6
  • 18
34
votes
2 answers

Why generic lambdas are allowed while nested structs with templated methods aren't?

As far as I understand - generic lambdas are transformed into objects of local scope structs with templated operator(). This makes generic lambda very powerful and easy to use tool. On the other hand one can create structs nested into the function,…
W.F.
  • 13,888
  • 2
  • 34
  • 81
27
votes
1 answer

Why does lambda auto& parameter choose const overload?

I'm trying to implement a class which wraps an arbitrary type and a mutex. To access the wrapped data, one needs to pass a function object as parameter of the locked method. The wrapper class will then pass the wrapped data as parameter to this…
Unda
  • 1,827
  • 3
  • 23
  • 35
26
votes
3 answers

What is the type of this self-applying factorial function?

I wrote an anonymous factorial function in C++ and compiled my code with g++4.9.2. It works well. However, I don't know the type of my function. #include #include using std::function; int main() { //tested at g++ 4.9.2 …
Alaya
  • 3,287
  • 4
  • 27
  • 39
18
votes
2 answers

Is it legal to explicitly specify a generic lambda's operator() template arguments?

Is the following C++ code standard compliant? #include int main() { [](auto v){ std::cout << v << std::endl; }.operator()(42); } Both clang++ 3.8.0 and g++ 7.2.0 compile this code fine (the compiler flags are -std=c++14 -Wall…
Constructor
  • 7,273
  • 2
  • 24
  • 66
16
votes
2 answers

Async void lambda expressions

A quick google search will tell you to avoid using async void myMethod() methods when possible. And in many cases there are ways to make it possible. My question is basically an offshoot of this best practice: What does the lambda expression below…
bwall
  • 984
  • 8
  • 22
15
votes
1 answer

Converting a forwarding lambda to a function pointer

Here are two things that work. We can instantiate a forwarding function template to get a function pointer taking an lvalue: template void f(T &&) {} void(*p)(int &) = f; // Cool! We can also convert a non-capturing generic lambda taking…
Quentin
  • 62,093
  • 7
  • 131
  • 191
15
votes
3 answers

Template functions versus named lambdas with auto parameters

What are the differences between template void func( T t ) { /* ... */ } and the C++14 alternative using lambdas with auto parameters? auto func = []( auto t ) { /* ... */ } Which one should be preferred?
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
14
votes
3 answers

Count the number of arguments in a lambda

I need to know the exact number of arguments that a lambda has. I do not care for their types, I just need a count. auto lambda0 = [&]() { ... }; auto lambda1 = [&](int32_t a) { ... }; auto lambda2 = [&](int32_t a, auto b) { ...…
Rick de Water
  • 2,388
  • 3
  • 19
  • 37
13
votes
4 answers

Is it possible to pass generic lambda as non-template argument

I have a toy example that I'd like to modify architecturally to remove type dependency of Processor on EmitterT: #include #include using namespace std; struct Emitter { void e(int) { cout << "emitting int\n";} void…
luk32
  • 15,812
  • 38
  • 62
11
votes
1 answer

clang vs gcc: variadic lambda captures

I am trying to capture a variadic lambda argument inside a inner lambda and use it there. As an example, consider this code: int main () { auto first = [&] (auto&&... one) { auto second = [&] (auto&&... two) { return ((one *…
nnolte
  • 1,628
  • 11
  • 25
11
votes
2 answers

Enabling `-std=c++14` flag in Code::Blocks

I have installed Code::Blocks for Windows and want to compile C++14 code like generic lambdas but the binary version of Code::Blocks that I've installed from codeblocks.org doesn't support the flag -std=c++14. How do I update the compiler and enable…
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
1
2 3 4 5 6 7 8