Using `std::function` to call non-void function

后端 未结 3 781
一个人的身影
一个人的身影 2020-11-28 15:53

A while ago I used std::function pretty much like this:

std::function func = [](int i) -> int { return i; };
3条回答
  •  伪装坚强ぢ
    2020-11-28 16:11

    This looks like it may be ok for anonymous functions.

    Quote from http://www.alorelang.org/release/0.5/doc/std_function.html (this is not from the C++ standard library, however it looks like they are using something similar in there bindings down to C++)

    Function objects can only be created with a function definition, an anonymous function expression or by accessing a bound method using the dot (.) operator.

    Another way this could possibly be done is by storing the function pointer in auto as seen here: http://en.wikipedia.org/wiki/Anonymous_function (C++ section)

提交回复
热议问题