C++11 has both lambda's and std::function<>, but unfortunately, they have different types. One consequence is that one cannot directly use lambda's in higher order functions such as map in lisp. For example, in the following code
#include <vector>
#include <functional>
using namespace std;
template <typename A,typename B>
vector<B> map(std::function<B (A)> f, vector<A> arr) {
vector<B> res;
for (int i=0;i<arr.size();i++) res.push_back(f(arr[i]));
return res;
}
int main () {
vector<int> a = {1,2,3};
map([](int x) -> int { return x;},a); //not OK
auto id_l = [](int x) -> int { return x;};
map(id_l,a); //not OK;
function<int (int)> id_f = id_l;
map(id_f,a); //OK
return 0;
}
, directly using lambda as in line 2 of main() won't work. g++ -std=c++11 testfunc.cpp
returns `... testfunc.cpp:14:37: note: 'main()::__lambda0' is not derived from 'std::function'.
C++11 type inferencing fails as well, as you can see if one stores the lambda to an auto variable and then use it, the type information is still lost, probably due to type erasure and reasons of small performance penalty (as I was told: why do lambda functions in c++11 not have function<> types?).
What does work is to store the lambda in a std:function<> typed variable and use that variable. This is rather inconvenient and kind of defeats the purpose of using lambda's in functional programming in C++11. For example, one cannot manipulate lambda's in place with stuff like bind or flip, and instead has to store the lambda to a variable first.
My question is, is it possible (and how) to overcome this issue and make line#2 of main() legal, e.g. by overwriting some typecast operators? (Of course, this means I don't care about the small performance penalty involved with using/not using type erasure.)
thanks in advance.
--- EDIT ---
To clarify, the reason I use std::function
rather than a generic type parameter for the functional parameter is that std::function
has exact type information, while a generic type parameter as in template <typename F> map(F f, ...)
contains no type information. Also, as I finally figured out, each lambda is its own type. So type erasure wasn't even a issue in the incompatibility between a lambda and its matching std::function
object.
---Update---
There are already two answers about how to make the map function above work or how to improve them. Just to clarify. My question isn't about how to make map work. There are plenty of other use cases involving using the std::function<> typed parameters, which I think can at least make the code more readable and make type inferencing easy. The answers so far are about how not to use std::function<> as parameters. My question was about how to make such a function (with std::function<> typed parameters) accept lambda's automatically.
-- Update 2 ---
In response to comments, here is a example of practical case where the type information in std::function<> COULD be useful. Suppose we want to implement a C++ equivalent of fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
in OCaml (http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html).
With std::function<>, one can do
//approach#1
template <typename A,typename B>
B fold_right(std::function<B (A, B)> f, vector<A> arr, B b) {
...
}
It is clear from above what f
is, and what it can or cannot take. Maybe, one can also use
//approach#2
template <typename A,typename B, typename F>
auto fold_right2(F f, vector<A> arr, B b) -> decltype(f(???)) {
...
}
But, this is becoming kind of ugly as you try to figure out what to put in the decltype
. Also, what exactly does f
take, and what's the correct way to use f
? From the point view of readability, I guess the reader of the code can only figure out what is f (a function or scalar) and the signature of f by INTERPRETING the implementation in the function body.
That is what I don't like and that's where my question comes from. How to make approach#1 work conveniently. For example, if f
represents addition of two numbers, approach#1 works if you create a function object first:
std::function<int (int, int)> add = [](int x, int y) -> int { return x + y; }
fold_right(add,{1,2,3},0);
Efficiency issues aside, the above code is inconvenient BECAUSE std::function cannot accept lambda's. So,
fold_right([](int x, int y) -> int { return x + y; },{1,2,3},0);
will not work currently in C++11. My question is specifically about if it is possible to make functions like fold_right
defined above accept lambda's directly. Maybe it's too much to hope for. I hope this clarifies the question.
Why would you want to create a dynamic indirection via
std::function<...>
in the first place? Just templatize on the function object and you are sorted:In fact, there isn't really any need for nailing the container type either and you probably want to pass it by [
const
] reference as well:Finally, please note that the standard C++ library already as a "map" function. It just happens to be spelled
std::transform()
and has an interface which fits the generic approach in C++ better:Your map function is broken. Do not use
std::function
unless you cannot use a template; and in this instance, you most assuredly can. You don't needB
as a template parameter becausedecltype
can give it to you, and you don't need the argument type to actually be astd::function
at all.For the record, this is ignoring everything else wrong with your map function.
Actually you could do it and even better (faster, cheaper) than with std::function. It has a heap alloc and a virtual function call. Its needed only for type erasure (to accept ANY CALLABLE with same signature). But for lambdas you don't need this (costly) flexibility. Just use lambda wrapper class
Finally figured out a generic wrapper function
make_function
(in current c++11) for converting any lambda to its correspondingstd::function
object with type deduction. Now instead of using ctor:which requires giving the same type information twice, the following succinct form works
Code is below:
--original answer--
To answer my own question after a couple of weeks' search (and getting chastised for using std::function<> as parameters), probably the best way I can find to have function<>-typed parameters accept lambda's (in c++11) is simply via explicit cast:
Or using ctor:
For comparison, if you have a function taking std::string (e.g.
void ff(string s) {...}
), it can takeconst char*
automatically. (ff("Hi")
would work). The automatic conversion from lambda tostd::function<>
does not similarly work in c++11 (, which is unfortunate, IMO).Hopefully, things will improve in c++14/1y when lambdas can be properly typed or better type-deduced.
You can't. Why do you suppose this is possible?
std::function
is part of the standard library, and it has no capabilities beyond what is possible with other class types.Moreover, by artificially restricting the solution space to function calls with a lambda as the argument and a
std::function<T>
as the parameter with deducedT
, there is nothing to possibly change. The argument will not match the parameter, and you've arbitrarily decided to forbid changing either.Given a function
dynamic_function_from_lambda
to encapsulate any lambda in astd::function
, you could perform the conversion explicitly either in the function call or the body of a function accepting lambda objects by deduction.Alternative A:
Alternative B:
The whole point of
std::function
is runtime polymorphism though, so if you're not using that, it is just wastefully inefficient.