C++11 bind 和 placeholders 用法

本文介绍了C++11中的bind和placeholders,虽然C++14已解决其部分局限,但在C++11中,bind在处理类型多态和移动语义上仍有优势。讲解了std::placeholders的使用,如_1、_2作为回调函数参数的占位符,并展示了std::is_placeholder用于检测占位符类型的示例,以及std::is_bind_expression用于判断是否为bind表达式的功能。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自由技艺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值