bind 这个东西是很鸡肋的,因为已经有了 lambda 表达式了,但是在 C++ 11 中 lambda 无法处理类型多态,bind可以;lambda 也不支持移动语义,bind 可以。
可喜的是,C++14 已经解决了上述两个问题,所以 bind 也就可以丢弃了。
1、std::placeholders
占位符 std::placeholders 是 C++ 11 的新特性,它一般和 bind 一起用。placeholders 定义如下:
namespace placeholders {
extern /* unspecified */ _1;
extern /* unspecified */ _2;
extern /* unspecified */ _3;
// ...
}
其中 _1, _2, _3 是未指定类型的数字对象。我们来看看它是如何使用的:
#include <functional>
#include <string>
#include <iostream>
void goodbye(const std::string& s)
{
std::cout << "Goodbye " << s << '\n';
}
class Object {
public:
void hello(const std::string& s)
{
std::cout << "Hello " << s << '\n';
}
};
int main()
{
using namespace std::placeholders;
using ExampleFunction = std::function<void(const std::string&)>;
Object instance;
std::string str("World");
// 示例 1:绑定类的成员函数
ExampleFunction f = std::bind(&Object::hello, &instance, _1);
f(str); // 等价于 instance.hello(str)
// 示例 2:绑定普通函数
f = std::bind(&goodbye, std::placeholders::_1);
f(str); // 等价于 goodbye(str)
// 示例 3:绑定 lambda 表达式
auto lambda = [](std::string pre, char o, int rep, std::string post) {
std::cout << pre;
while (rep-- > 0) std::cout << o;
std::cout << post << '\n';
};
std::function<void(std::string, char, int, std::string)> g =
std::bind(&decltype(lambda)::operator(), &lambda, _1, _2, _3, _4);
g("G", 'o', 'o'-'g', "gol");
}
输出结果为:
_1 用于代替 std::bind 中回调函数的第一个参数, _2 用于代替回调函数中的第二个参数,以此类推。
2、std::is_placeholder
std::is_placeholder 用于判断 T 是否为占位符,它有一个成员变量 value。如果 T 是 placeholder 类型,value的值为1代表 _1,2 代表 _2;如果 T 不是占位符,则 value 为 0。示例如下:
#include <iostream>
#include <functional>
int main () {
using namespace std::placeholders;
std::cout << std::is_placeholder<decltype(_1)>::value << '\n'; // 输出 1
std::cout << std::is_placeholder<decltype(_2)>::value << '\n'; // 输出 2
std::cout << std::is_placeholder<int>::value << '\n'; // 输出 0
return 0;
}
3、std::is_bind_expression
这个函数用于判断是否是 bind 表达式,有 value 成员,返回值是 true 或 false.