Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

c++ - How to make C++11 functions taking function<> parameters accept lambdas automatically

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

template <typename A, typename F> 
auto map(F f, std::vector<A> arr) -> std::vector<decltype(f(arr[0]))> {
    std::vector<decltype(f(arr[0]))> res;
    for (int i=0; i<arr.size(); ++i)
        res.push_back(f(arr[i]));
    return res;
}

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:

template <typename C, typename F> 
auto map(F f, C const& c) -> std::vector<decltype(f(*c.begin()))> {
    std::vector<decltype(f(*c.begin()))> res;
    for (auto const& value: c)
        res.push_back(f(value));
    return res;
}

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:

std::vector<int> result;
std::transform(a.begin(), a.end(), std::back_inserter(result),
               [](int x){ return x;});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...