这部分内容虽然属于标准库的一部分,但是从本质上来看,它却增强了 C++ 语言运行时的能力,这部分内容也相当重要,所以放到这里来进行介绍。
std :: function和std :: bind的主要用途之一是安全函数指针。网上有很多文章解释了函数指针在C / C ++中是如何有用的。例如,它们对于实现回调机制很有用。也就是说,你有一些功能需要很长时间才能执行,但你不想等待它返回然后你可以在单独的线程上运行该函数并给它一个函数指针,它将在它完成后回调。
这是一个如何使用它的示例代码
class MyClass {
private:
//just shorthand to avoid long typing
typedef std::function<void (float result)> CallbackType;
//this function takes long time
void longRunningFunction(CallbackType callback)
{
//do some long running task
//...
//callback to return result
callback(result);
}
//this function gets called by longRunningFunction after its done
void afterCompleteCallback(float result)
{
std::cout << result;
}
public:
int longRunningFunctionAsync()
{
//create callback - this equivalent of safe function pointer
auto callback = std::bind(&MyClass::afterCompleteCallback,
this, std::placeholders::_1);
//normally you want to start below function on seprate thread,
//but for illustration we will just do simple call
longRunningFunction(callback);
}
};
下面我们拆解开来分析。
std::function
Lambda 表达式的本质是一个函数对象,当 Lambda 表达式的捕获列表为空时,Lambda 表达式还能够作为一个函数指针进行传递,例如:
#include <iostream>
using foo = void(int); // 定义返回值为void,参数类型为int的函数指针
void functional(foo f) {
f(2);
}
int main() {
auto f = [](int value) {
std::cout << value << std::endl;
};
functional(f); // 函数指针调用,将进入函数内部 输出:2
f(1); // lambda 表达式调用 输出:1
return 0;
}
上面的代码给出了两种不同的调用形式,一种是将 Lambda 作为函数指针传递进行调用,而另一种则是直接调用 Lambda 表达式,在 C++11 中,统一了这些概念,将能够被调用的对象的类型,统一称之为可调用类型。而这种类型,便是通过std::function
引入的。
C++11 std::function
是一种通用、多态的函数封装,它的实例可以对任何可以调用的目标实体进行存储、复制和调用操作,它也是对 C++中现有的可调用实体的一种类型安全的包裹(相对来说,函数指针的调用不是类型安全的),换句话说,就是函数的容器。当我们有了函数的容器之后便能够更加方便的将函数、函数指针作为对象进行处理。例如:
#include <functional>
#include <iostream>
int foo(int para) {
return para;
}
int main() {
int a = 123;
a += 1;
int b = 220;
auto f = [=] { std::cout << a << std::endl; };
f(); //输出:124
std::cout << std::endl;
// std::function 包装了一个返回值为 int, 参数为 int 的函数
std::function<int(int)> func = foo;
int important = 10;
std::function<int(int)> func2 = [&](int value) -> int { //[&]或[=],隐式捕获
return 1 + value + important;
};
std::cout << func(10) << std::endl; //输出:10
std::cout << func2(10) << std::endl; //输出:21
}
std::bind/std::placeholder
C++11中提供了std::bind
,可以说是一种飞跃的提升,bind本身是一种延迟计算的思想,它本身可以绑定普通函数、全局函数、静态函数、类静态函数甚至是类成员函数。
std::bind
是用来绑定函数调用的参数的,它解决的需求是我们有时候可能并不一定能够一次性获得调用某个函数的全部参数,通过这个函数,我们可以将部分调用参数提前绑定到函数身上成为一个新的对象,然后在参数齐全后,完成调用。例如:
#include <functional>
#include <iostream>
int foo(int a, int b, int c) {
return a+b+c;
}
int main() {
// 将参数1,2绑定到函数 foo 上,但是使用 std::placeholders::_1 来对第一个参数进行占位
auto bindFoo = std::bind(foo, std::placeholders::_1, 1, 2);
// 这时调用 bindFoo 时,只需要提供第一个参数即可
std::cout<< bindFoo(3) <<std::endl; //输出:6 //a=3,b=1,c=2
std::cout << bindFoo(4,5) << std::endl; //输出:7 //a=4,b=1,c=2
auto bindFoo_1 = std::bind(foo, std::placeholders::_1, 6, std::placeholders::_2);
std::cout << bindFoo_1(7,8) << std::endl; //输出:21 //a=7,b=6,b=8
}
提示:注意 auto
关键字的妙用。有时候我们可能不太熟悉一个函数的返回值类型,但是我们却可以通过 auto
的使用来规避这一问题的出现。
那么什么时候会用到或者有什么好处呢?
每次我需要使用 std::bind
,我最终使用lambda代替。所以我什么时候应该使用 std::bind
?我刚刚从一个代码库中删除它,我发现lambdas总是更简单,更清晰 std::bind
。是不是 std::bind
完全没必要?它不应该在将来被弃用吗?我应该什么时候喜欢 std::bind
到lambda函数? (必须有一个原因,它与lambdas同时进入标准。)
这是lambda无法做到的事情:
std::unique_ptr<SomeType> ptr = ...;
return std::bind(&SomeType::Function, std::move(ptr), _1, _2);
Lambdas无法捕获仅移动类型;它们只能通过复制或左值引用来捕获值。虽然这是一个暂时的问题,正在积极解决C ++ 14。
“更简单,更清晰”也是一个问题。对于简单的绑定案例, bind
打字可以减少很多。 bind
也只关注功能绑定,所以如果你看到 std::bind
,你知道你在看什么。然而,如果你使用lambda,你必须查看lambda实现以确定它的作用。
最后,C ++不会因为某些其他功能可以执行的操作而弃用。 auto_ptr
因为它被弃用了 固有的危险 使用,并有一个非危险的替代品。
您可以使用std::bind
创建多态对象 ,但你不能用lambdas,即返回的调用包装器 (C++14中lambda表达式已经支持):std::bind
可以使用不同的参数类型调用
#include <functional>
#include <string>
#include <iostream>
struct Polly
{
template<typename T, typename U>
auto operator()(T t, U u) const -> decltype(t + u)
{
return t + u;
}
};
int main()
{
auto polly = std::bind(Polly(), std::placeholders::_1, 12);
std::cout << polly(13) << std::endl; // 输出:25
auto polly_1 = std::bind(Polly(), std::placeholders::_1, "confusing");
std::cout << polly_1(std::string(" this is ")) << std::endl; // 输出:this is confusing
auto polly_2 = [p = Polly{}](auto t) { return p(t, 12); };
std::cout << polly_2(13) << std::endl; // 输出:25
auto polly_3 = [p = Polly{}](auto t) { return p(t, "confusing"); };
std::cout << polly_3(std::string(" this is ")) << std::endl; // 输出:this is confusing
return 0;
}
std::bind
提出是为了 部分功能应用,它比编写仿函数类更简洁。
我不建议仅仅因为您不喜欢API而使用它,但它具有潜在的实际用途,例如:
not2(bind(less<T>, _2, _1));
是一个小于或等于的功能(假设总订单,等等)。这个例子通常不是必需的,因为已经有了 std::less_equal
(它用的是 <=
操作员而不是 <
,如果它们不一致,那么你可能需要这个,你可能还需要用一个线索访问该类的作者)。不过,如果您使用的是函数式编程,那就会出现这种转换。
总结
std :: bind在提议包含boost绑定后被投入库中,主要是部分功能专用,其中你可以修复一些参数并在运行中更改其他参数。现在这是用C ++编写lambda的库方式。正如Steve Jessop所回答的那样
既然C++11支持lambda函数,我就不想再使用std::bind了。与库特性相比,我更愿意使用语言特性(部分专门化)。
函数对象是多态函数。基本思想是能够互换地引用所有可调用对象。
我会向您指出这两个链接以获取更多详细信息:
C ++ 11中的Lambda函数: http://www.nullptr.me/2011/10/12/c11-lambda-having-fun-with-brackets/#.UJmXu8XA9Z8
C ++中的可调用实体: http://www.nullptr.me/2011/05/31/callable-entity/#.UJmXuMXA9Z8