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

Hard error when using std::invoke_result_t with a generic lambda

I have a container-like class with a method that works similarly to std::apply. I would like to overload this method with a const qualifier, however when I try to invoke this method with a generic lambda I get a hard error from instantiation of…
r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
4
votes
2 answers

Forward variadic argument to thread with lambda

I'm having trouble finding how use std::thread() with lambdas. Namely, with having a variadic argument lambda receive the arguments by forwarding. As an example: template auto foo(T&&... t){ [](T&&... t){}(std::forward(t)...);…
4
votes
2 answers

How to get types from variadic parameter pack in generic lambda?

I'm trying to write a function that will return a generic lambda with variadic arguments, where the lambda checks that one of the arguments is equal to a specific value. Here's (roughly) what I'm trying to do: template
Chris Vig
  • 8,552
  • 2
  • 27
  • 35
3
votes
0 answers

MSVC construct function bugged?

I am working with latest version of MSVC, clang and gcc. The two last ones accept the following code, but MSVC rejects it. Is MSVC right? Or is it a bug? struct A { int a, b, c; }; template constexpr auto construct_impl() { …
Antoine Morrier
  • 3,930
  • 16
  • 37
3
votes
2 answers

lambda fold expression get index

Is there a simple way to add the current index of the arg As in include it in the lambda call so I don't need to use index++ template void Function(Args... args) { std::size_t size = sizeof...(Args); std::size_t index =…
3
votes
2 answers

How to get return type and parameter type of a generic lambda?

I want to get return type and parameter type of a lambda. Is it possible to figure out the parameter type and return type of a lambda?give a solution.But it does not work for generic lambda. template struct function_traits :public …
haskeller
  • 141
  • 4
3
votes
2 answers

How to pass generic lambda into function

I have the following C++ code: template void MetaTypeHandler(T1 lambda1, T2 lambda2, T3 lambda3) { lambda1(1); lambda2('x'); lambda3(true); } int main() { auto f = [] (auto x) {}; …
Bulat
  • 2,435
  • 1
  • 15
  • 15
3
votes
2 answers

Use lambda to modify references identified by a packed parameter?

I'm trying to create a nice modify function of an entity's components in my WIP small "game framework". However I'm stuck at creating the function when trying to modify more than one component (using packed parameters) This is my function for a…
Frans Bstrom
  • 195
  • 1
  • 12
3
votes
1 answer

Get argument type of lambda with auto specifier

I have a meta function which gives me the type of the I-th argument of a lambda/function: #include #include namespace details { //! Spezialization for Funktion-Pointers template
3
votes
1 answer

Overloaded template functions deduction error

I struggle to get this right. I want to create overloaded template functions hell that would make such calls possible and correct (GMock): ASSERT_EQ(min(1, 2), 1); ASSERT_EQ(min(std::less<>(),3,2), 2); auto abs_comp = [](auto el1, auto el2){ …
Leśny Rumcajs
  • 2,259
  • 2
  • 17
  • 33
3
votes
3 answers

Perfect forwarding of lambda arguments to member function, where member function is a non-type template parameter

Context I want to wrap a member function and a specific object into a function object (which I'll use as a callback later). I would like to write this wrapping function once for different member functions and objects especially because my actual…
3
votes
1 answer

C++14 Generic lambdas in header file

I have set of functors like the following: const auto add = [](const auto& x) { return [=](const auto& n) { return n + x; }; }; Is it right to store them in a header file? (any side effects?)
Jonatan Kłosko
  • 205
  • 1
  • 6
2
votes
3 answers

ConcurrentModificationException with very limited iterations

I am trying to learn caching and I tried this: public static BiFunction cached(BiFunction bifunc) { Cache, R> cache = new Cache, R>(DEFAULT_CACHE_SIZE); System.out.println("Cache Size:…
Shirofuji
  • 36
  • 8
2
votes
1 answer

Is it possible to chain predicates with `and` using different type parameter?

I am learning Java 8 lambdas. Can I join two predicates, with different type parameter, using and method in Predicate interface? This is my code: Predicate pc = (iv) -> iv > 20; Predicate pL = (is) -> is.length() >…
Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82
2
votes
6 answers

Generic Lambdas

What is the difference between this line of code (Code 1) auto l1 = [](auto a) { static int l = 0; std::cout << a << " " << ++l << std::endl; }; this line (Code 2) static int l = 0; auto l1 = [](auto a) { std::cout << a << " " << ++l << std::endl;…
Goddrew
  • 125
  • 2
  • 5