Pass lambda parameter to std::function parameter without intermediate variable


Anakhand

This might be something like "I can't pass a lambda as a std::function" , but it's actually passing parameters std::functionby value , so the question doesn't apply. I have the following function defined.

template<typename T>
std::vector<T> countSort(const std::vector<T> &v, std::function<int(T)> keyFunc, int n);

The second parameter is what to std::functionmap Tto int(pass by value).

When calling this function, I want to use a lambda expression like this:

std::vector<int> v;
[...]
v = countSort(v, [](int x) { return x; }, 10);

But template argument deduction fails because " main()::<lambda(int)>is not derived from std::function<int(T)>" . It works if I specify template parameters, or introduce an intermediate variable of type std::functionfor the lambda expression :

std::function<int(int)> lambda = [](int x) { return x; };
v = countSort(v, lambda, 10);

Why can't I do the former? I give the exact same information to the compiler. If it's possible to convert the value of a type lambda<int>to std::function<int(int)>assign it to a variable, why can't it be converted directly from the lambda<int>parameter type, which is std::function<T(int)>- and taking into account vthe type , one std::vector<int>should know ? The whole reason I want to use a lambda expression is precisely because it's an expression , so I should be able to write it inline in the function call parameter list without having to name it or assign it to a variable.Tint

songyuanyao

The problem is that template parameter deduction doesn't take into account implicit conversions (from lambda to std::function), which causes the deduction of the Tsecond function parameter to keyFuncfail.

Type deduction doesn't take into account implicit conversions (except for the type adjustments listed above): this is the job of overload resolution , which happens later.

You can use (since C++20) to exclude the second function argument from the inference. E.gstd::type_identity

template<typename T>
std::vector<T> countSort(const std::vector<T> &v, std::function<int(std::type_identity_t<T>)> keyFunc, int n);

BTW: if your compiler doesn't support std::type_identityit, it's not hard to create one.

See the non-inferential contextstd::type_identity for how this works :

(emphasis mine)

P Types, templates and non-type values ​​used for composing do not participate in template parameter deduction in the following cases , but instead use template parameters deduced elsewhere or explicitly specified . Template parameter deduction fails if the template parameter is only used in an undeduced context and is not specified explicitly.

  1. Use nested name specifiers for types specified by qualified -id (everything to the left of the scope resolution operator ::) :

Related


Pass input parameter to std::function in lambda function

Shohreh I have a class that has a public std::functionmember like this: class B { public: B(std::function<void(void)> _func = NULL) : m_function(_func) { } std::function<void()> m_function; }; I have a class Xwith member functions SomeFunction: class

Pass input parameter to std::function in lambda function

Shohreh I have a class that has a public std::functionmember like this: class B { public: B(std::function<void(void)> _func = NULL) : m_function(_func) { } std::function<void()> m_function; }; I have a class Xwith member functions SomeFunction: class

Pass lambda function as parameter

Zenoo I am trying to create a Javascript equivalent of a script in Java .Array#map I have been able to do ArrayList<String> data = myList.stream().map(e -> { return "test "+e; }).collect(Collectors.toCollection(ArrayList::new)); Here, myListis the initial

Pass a function as a parameter (Lambda)

not_a_number I am trying to understand Java 8's Lambda expressions. In the example I want to parse many files. For each file I need to create a new instance of a specific template (same for all files in one pass). If I understand correctly, that's the beauty o

Pass lambda function as parameter

Zenoo I am trying to create a Javascript equivalent of a script in Java .Array#map I have been able to do ArrayList<String> data = myList.stream().map(e -> { return "test "+e; }).collect(Collectors.toCollection(ArrayList::new)); Here, myListis the initial

Pass array as function parameter without defining variable

Abhigyan kumar How can we pass an array directly to a function in C? E.g: #include <stdio.h> void function(int arr[]) {}; int main(void) { int nums[] = {3, -11, 0, 122}; function(nums); return 0; } Instead of this, can we write something like fu

Function parameter; pass the variable name without quotes

Benjamin Schlegel The problem is similar to this: Pass data.frame column names to function I have a function: optimal_cutpoint <- function(data, ..., choice){ selection <- dplyr::select(data, ...) choice <- data[[choice]] # do something with those two ob

Pass array as function parameter without defining variable

Abhigyan kumar How can we pass an array directly to a function in C? E.g: #include <stdio.h> void function(int arr[]) {}; int main(void) { int nums[] = {3, -11, 0, 122}; function(nums); return 0; } Instead of this, can we write something like fu

Function parameter; pass the variable name without quotes

Benjamin Schlegel The problem is similar to this: Pass data.frame column names to function I have a function: optimal_cutpoint <- function(data, ..., choice){ selection <- dplyr::select(data, ...) choice <- data[[choice]] # do something with those two ob

Pass array as function parameter without defining variable

Abhigyan kumar How can we pass an array directly to a function in C? E.g: #include <stdio.h> void function(int arr[]) {}; int main(void) { int nums[] = {3, -11, 0, 122}; function(nums); return 0; } Instead of this, can we write something like fu

Pass array as function parameter without defining variable

Abhigyan kumar How can we pass an array directly to a function in C? E.g: #include <stdio.h> void function(int arr[]) {}; int main(void) { int nums[] = {3, -11, 0, 122}; function(nums); return 0; } Instead of this, can we write something like fu

Pass lambda as template function parameter

Emle Why doesn't the following code compile (in C++11 mode)? #include <vector> template<typename From, typename To> void qux(const std::vector<From>&, To (&)(const From&)) { } struct T { }; void foo(const std::vector<T>& ts) { qux(ts, [](const T&) { ret

Pass lambda expression as function parameter

User 3 A function like this bool decide(bool x)can be passed as a functor as a parameter in a function as: foo(Func<bool,bool> lambda) We can have lambda expressions ()=>{int x=8; x=x+2;}that take nothing and return nothing. Lets say I want to pass functions

Pass the OrderBy lambda function as a parameter

style Suppose I have a model Userwith several properties named Nameand Age. I would like to know Userthe type of a lambda function that takes a and returns one of the properties so that I can use the lambda function in that OrderBymethod . using System; using

Pass lambda as template function parameter

Emle Why doesn't the following code compile (in C++11 mode)? #include <vector> template<typename From, typename To> void qux(const std::vector<From>&, To (&)(const From&)) { } struct T { }; void foo(const std::vector<T>& ts) { qux(ts, [](const T&) { ret

Pass lambda as template function parameter

Emle Why doesn't the following code compile (in C++11 mode)? #include <vector> template<typename From, typename To> void qux(const std::vector<From>&, To (&)(const From&)) { } struct T { }; void foo(const std::vector<T>& ts) { qux(ts, [](const T&) { ret

Pass lambda as template function parameter

Emle Why doesn't the following code compile (in C++11 mode)? #include <vector> template<typename From, typename To> void qux(const std::vector<From>&, To (&)(const From&)) { } struct T { }; void foo(const std::vector<T>& ts) { qux(ts, [](const T&) { ret

Pass lambda expression as function parameter

User 3 A function like this bool decide(bool x)can be passed as a functor as a parameter in a function as: foo(Func<bool,bool> lambda) We can have lambda expressions ()=>{int x=8; x=x+2;}that take nothing and return nothing. Lets say I want to pass functions