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
6
votes
3 answers

Templated lambdas in C++17 without an auto argument

I have a class Derived that inherits from class Base: template class Base { protected: ResourceType* resource; public: void set_resource(ResourceType* resource) { this->resource = resource; } }; template…
Sagar Jha
  • 1,068
  • 4
  • 14
  • 24
6
votes
2 answers

C++ Lambdas with Ellipses in the Parameter List

I'm working on a library which uses lambdas for delineating the scopes of expression terms. Because the library has to hand out unique integer numbers to identify each variable, it is ideal if the library, not the user, constructs the variables and…
Dennis
  • 2,607
  • 3
  • 21
  • 28
6
votes
1 answer

Generic lambda cannot be used within a namespace?

Consider the following piece of code #include #include namespace A { template struct X { using Function = std::function; static Function f; }; template
rafalc
  • 203
  • 5
  • 9
6
votes
1 answer

Template deduction for variadic template lambda arguments

Given the following variadic template: template void fun(void(*f)(Params...), Params... params) { f(params...); } int main() { fun(+[](int a, int b) {}, 2, 3); } For now when invoking fun with a lambda I need to specify…
Marcin Król
  • 1,555
  • 2
  • 16
  • 31
6
votes
2 answers

Is there a std::function type or similar for lambda with auto parameter?

When I assign a lambda to an explicitly typed variable (for example when it is recursive, to capture the function in itself), I use std::function. Consider this silly "bit counting" function as an example: std::function f; f = [&f](int x){…
leemes
  • 44,967
  • 21
  • 135
  • 183
5
votes
1 answer

Why does g++ 10.1 complain about a named lambda in a header file and others do not?

I have a header file with a named lambda that I use for measuring the execution time of some functions (the lambda is partially a result of this question How to Write a Lambda Wrapping a Function with Optional Return Value). It resides in a header…
Benjamin Bihler
  • 1,612
  • 11
  • 32
5
votes
2 answers

How to deduce the return type of a lambda?

I want to mimic Ruby's map() method in C++. I am struggling to figure out the return type automatically: #include #include #include #include typedef std::string T2; template
5
votes
2 answers

class template fails to compile when named lambda is used as template class argument or constructor argument

I'm currently experimenting with class template programming and I came across this weird behavior that I cant understand when passing a named lambda as its argument. Could somebody explain why (1) & (2) below does not work? template
diaz
  • 51
  • 1
5
votes
3 answers

c++, a generic recursive template function to traverse tree like structures

I tried to traverse tree like structures with a generic recursive function without defining a recursive function in global each time for each structures. //structure #1 class CA { public: std::string name; std::vector vecChild; }; and…
sainimu78
  • 63
  • 6
5
votes
3 answers

Is it possible to make a template variable in lambda signature generic?

Suppose you have a function that takes a std::vector of any type and processes it in some way: template void foo(std::vector &vec) { // work with vec } Since C++14, we are able to achieve the same thing with lambdas. In this…
Fureeish
  • 12,533
  • 4
  • 32
  • 62
5
votes
2 answers

How can a concise closure be written for a generic method?

I want to write an implementation of a functional, non-generic interface which has a generic method. The implementation needs to be an inline closure and concise. As a simplified example @FunctionalInterface interface Fn { R fn(R…
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
5
votes
1 answer

Detecting parameter types from generic lambda - compile error with GCC

I wrote some code that retrieves the types of the non-auto parameters when given a generic lambda function. As you can see in the code below, the idea is to call the connect function with a generic lambda and provide arguments for the auto…
texus
  • 291
  • 1
  • 3
  • 12
4
votes
2 answers

lazy instantiation for lambda expression

I want to access foo::func() in the lambda expression but the class foo is declared but not defined at this point. Is there any way to lambda expression lazily ? If I replace the lambda expression with the equivalent function object, then I can do…
Takatoshi Kondo
  • 3,111
  • 17
  • 36
4
votes
1 answer

std::function accepts lambda functions with arguments of different passing type (by ref, by val)

please look at the following code #include #include #include int main() { std::function theFunc; std::string foo = "0"; theFunc = [](std::string a) { a = "1"; }; // this compiles…
Henk
  • 750
  • 10
  • 21
4
votes
4 answers

Implementing middleware function using member functions with lambdas/bind

I have a function, Post(), that takes two arguments - a std::string path to listen for requests on, and a std::function to handle an incoming request. Note that I can't modify Post(). For…
Zpalmtree
  • 1,339
  • 8
  • 14