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
1
vote
1 answer

c++ lambda: Currying sum function: returns different results using capture by value vs by reference

I have a very simple recursive lambda which calculates sum of given 2 numbers: auto sum_v1 = [](auto first){ return [first](auto second){ return first + second; }; }; sum_v1 (1)(2); // returns 3, OK Now the same function, using capture by…
1
vote
1 answer

obtain function pointer of bound/inferred capturing lambda with static local variable

Is it possible to obtain a C style function pointer of a capturing lambda using static local variables? I am trying to bind the first the argument of the derived function to the original parameter template class entry_hook { public: …
Adam W
  • 317
  • 1
  • 3
  • 15
1
vote
1 answer

Generic lambdas and the unary + operator

From my understanding, the + operator before a lambda expression resolves it to a function pointer overload. (Post) However I do not understand quite understand why it does not work with generic lambdas. For example: auto foo = +[](int a) {…
Ammar Husain
  • 1,789
  • 2
  • 12
  • 26
1
vote
1 answer

Making sense of nested lambda expression

My question pertains to the following nested lambda expression, provided as an example under Lambda expressions // generic lambda, operator() is a template with one parameter auto vglambda = [](auto printer) { return [=](auto&&... ts) // generic…
Vinod
  • 925
  • 8
  • 9
1
vote
3 answers

Why are C++20 template lambdas using typename keyword?

I understand the consistency argument, but most of the parameters for templates are types so I feel that since lambdas are meant to be concise way of defining a struct it probably should have been defaulted to typename/class(you would still need to…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
2 answers

Template argument not matched in variadic tuple invoke function

Why can the compiler not match the template parameter F in the invoke() Function. Is there anywhere a non-deduced context I am not aware of?? And how can it be fixed? Live // Invoke Function with all tuple arguments. template
1
vote
1 answer

What's type deduced by auto in lambda expression used to modify a vector of type bool (special container)

I want to change the state of one variable in std::vector using a function by reference but this doesn't work because std::vector is a proxy object. However when I try to change it using lambda expression I could modify it. Here's an…
Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
1
vote
3 answers

How to Write a Lambda Wrapping a Function with Optional Return Value

I have tried to write a lambda that measures the execution time of arbitrary functions. With a lot of help I have managed that for C++14 and functions having a return value, see Measure execution time of arbitrary functions with C++14 lambda. Then I…
Benjamin Bihler
  • 1,612
  • 11
  • 32
1
vote
1 answer

Incorrect copy constructor while passing argument to generic lambda

Consider this piece of code: template < auto What > constexpr auto Identity = [](auto&&...) { return What; }; struct Ban { Ban() = default; Ban(const Ban& ban) = delete; Ban( Ban&& ban) = delete; }; int main() { Ban ban; …
Vahagn
  • 4,670
  • 9
  • 43
  • 72
1
vote
1 answer

Generic lambdas and binary size / code bloat

What is the difference in the resulting binary, when we compare this code: struct S { template void operator()(Args... args) { /* ... */ } }; // And then inside some block: S s; s(42); s(3.14, "Hi!"); s("Hi!",…
basteln
  • 2,483
  • 2
  • 19
  • 20
1
vote
0 answers

name-lookup in generic lambdas in intel icc

What are the name-lookup rules in a generic lambdas, if in the surrounding scope names from a different namespace are imported? namespace ns { template void bar2(F f) { f(0); } template void bar1(F f) { bar2(f); …
spraetor
  • 441
  • 4
  • 13
1
vote
2 answers

Passing a callable to a object that builds some kind of multi-index container

I'm trying to write a container that is able to categories objects and store pointers for which a categorisation function is true. My problem is, that it does not compile and since I'm inexperienced in regard to callables like std::function or…
dani
  • 3,677
  • 4
  • 26
  • 60
1
vote
3 answers

Generic lambdas: syntactic sugar or not?

Do C++14 generic lambdas bring a real improvement to the language or they are a kind of syntactic sugar? Whether there are some situations where [](auto param1, auto param2, /* ... */ auto paramN) { return /* ... */; } cannot be replaced…
Constructor
  • 7,273
  • 2
  • 24
  • 66
1
vote
2 answers

C++14 combining generic lambdas and variable templates

I know about generic lambdas, and I know about variable templates, but, what does this do? Is it even allowed? template auto f = [](auto a, T b){ /**/ }; If it's allowed, can it be used as expected? That is, as f(var_a, var_b)?
LB--
  • 2,506
  • 1
  • 38
  • 76
0
votes
2 answers

How to have a function pointer to a generic lambda?

As the title says, how can I have the following intent expressed in code ? The requirements being the function pointer takes any type of arguments. Variadic doesn't work because of std::string. https://godbolt.org/z/1E1szT Note that I cannot…
puio
  • 1,208
  • 6
  • 19