std::function<> 模板到底是什么? 可以简单理解为对应 C 语言中的函数指针。入参或绑定的参数如果是原始指针或引用时,需要特别注意其生存周期。
关于函数类模板,学习《Effective.Modern.C++》P39
And maybe now you’re thinking “What’s a
std::functionobject?” So let’s clear that up
boost::empty_value 的意义?
待补充
获得 std::function 对象
Lambda expressions
Constructs a closure: an unnamed function object capable of capturing variables in scope. 摘自 lambda
Function objects
A function object is any object for which the function call operator is defined. 摘自 functional
std::function,推荐查看其 Example
Class template
std::functionis a general-purpose polymorphic function wrapper. Instances ofstd::functioncan store, copy, and invoke any CopyConstructible Callable target – functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members. 摘自 function
需要强调的是 C++ 标准未对 std::bind 表达式的返回类型做出定义,其返回值并不对应我们熟悉的任何类型,但可以直接赋值给 std::function<>
1 | std::function<int(int)> func = std::tolower; |
模板参数
1 | // ERR,末尾多余的 const 造成报错内容在千里之外 |
递归调用
为什么 function<> 对象递归调用自身不能使用 auto?
vc2015 IntelliSence 提示
使用
auto类型说明符声明的变量不能出现在其自身的初始值设定项中