资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++STL内std::{bind/tuple/function}的使用方法

这篇文章主要介绍C++ STL内 std::{bind/tuple/function}的使用方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联建站主营景洪网站建设的网络公司,主营网站建设方案,成都app软件开发公司,景洪h5微信平台小程序开发搭建,景洪网站营销推广欢迎景洪等地区企业咨询

基本逻辑思考

首先是实现 function,这个比较简单,重载 operator() 就好,这里只实现对函数指针的包装

其次是实现 tuple,这个会比较绕,通过模板取第一个参数,然后用剩下的参数继续生成  tuple并继承,是一种递归的思想

有了 tuple 就要有 get(),这个就更比较绕了,首先是需要类似的方式实现获得 tuple 的值类型与元组类型,然后通过强制类型转换,获取对应的层级的 value

接下来是 bind,首先要解决的就是如何保存创建时的参数列表,这里就用到 tuple 来保存了

奇技淫巧还是运行函数时取相应的元组的对应位置的值,还是类似的方式,通过特化模板,公式是  => ,比如 3 最后会生成 0 0 1 2 那么抛弃第一个,并用来展开元组,传递给函数指针

最重要的来了,就是如何实现 placeholders,简单来说就是在上一步的 operator() 增加传入参数,并制造成元组 r_args,然后带进一个 _unwrap_tuple 类,这里会重载 operator[] 根据传入数据结构,如果是 _placeholders 那么取 r_args 相应的 index 位置,否则会直接  return

代码

不多说,还是直接放代码,仅作为参考,有写的不好的地方轻喷

/*
 * Author: SpringHack - springhack@live.cn
 * Last modified: 2020-02-19 10:16:17
 * Filename: main.cpp
 * Description: Created by SpringHack using vim automatically.
 */
#include 

namespace dosk { // begin namespace dosk

// function
template 
class function;

template 
class function {
 private:
  Result (*function_)(Args...);
 public:
  typedef Result return_type;
  function() = default;
  function(Result (*fn)(Args...)) : function_(fn) {};
  Result operator()(Args... a) {
   return function_(a...);
  }
  function& operator=(Result (*fn)(Args...)) {
   function_ = fn;
   return *this;
  }
};

// tuple
template 
class tuple;

template 
class tuple : public tuple {
 public:
  HEAD value;
  tuple(HEAD head, LIST... list) : tuple(list...), value(head) {};
};

template <>
class tuple<> {};

// tuple get
template 
class _tuple_type;

template 
class _tuple_type> {
 public:
  typedef typename _tuple_type>::value_type value_type;
  typedef typename _tuple_type>::tuple_type tuple_type;
};

template 
class _tuple_type<0, tuple> {
 public:
  typedef HEAD value_type;
  typedef tuple tuple_type;
};

template 
typename _tuple_type>::value_type get(tuple t) {
 typedef typename _tuple_type>::value_type value_type;
 typedef typename _tuple_type>::tuple_type tuple_type;
 value_type rv = ((tuple_type)t).value;
 return rv;
}

// bind
template 
class _tuple_index {};

template 
class _make_indexs : public _make_indexs {};

template
class _make_indexs<0, indexs...> {
 public:
  typedef _tuple_index index_type;
};

namespace placeholders {

template 
class _placeholders {};

_placeholders<0> _1;
_placeholders<1> _2;
_placeholders<2> _3;
_placeholders<3> _4;
_placeholders<4> _5;
_placeholders<5> _6;
_placeholders<6> _7;
_placeholders<7> _8;
_placeholders<8> _9;
_placeholders<9> _10;

template 
class _unwrap_tuple {
 public:
  tuple r_args; 
  _unwrap_tuple(tuple r_args) : r_args(r_args) {};
  template 
  R operator[](R r) {
   return r;
  }
  template 
  auto operator[](placeholders::_placeholders) {
   return get(r_args);
  }
};

};

template 
class bind_t {
 public:
  typedef typename _make_indexs::index_type _indexs;
  typedef typename Func::return_type return_type;
  Func func;
  tuple args;
  bind_t(Func func, Args... args): func(func), args(args...) {}
  template 
  return_type operator()(RArgs&&... _r_args) {
   tuple r_args = tuple(_r_args...);
   return run(_indexs(), r_args);
  }
  template 
  return_type run(_tuple_index, tuple r_args) {
   return func(unwrap_args(r_args)...);
  }
  template 
  auto unwrap_args(tuple r_args) {
   placeholders::_unwrap_tuple _u_a(r_args);
   auto _m_a = get(args);
   return _u_a[_m_a];
  }
};

template 
bind_t bind(Func& func, Args&&... args) {
 return bind_t(func, args...);
}

}; // end namespace dosk



// Test code
std::string test_func(int a, const char * b) {
 return std::to_string(a) + std::string(b);
}

std::string test_bind_args(int a, int b, int c, int d, int e) {
 return std::to_string(a) + std::to_string(b) + std::to_string(c) + std::to_string(d) + std::to_string(e);
}

int main() {
 // Test tuple
 dosk::tuple t(123, "456");
 std::cout << dosk::get<0>(t) << dosk::get<1>(t) << std::endl;
 // Test function
 dosk::function closure_1 = test_func;
 std::cout << closure_1(123, "456") << std::endl;
 // Test bind
 dosk::function closure_2 = test_bind_args;
 auto binder = dosk::bind(closure_2, 1, dosk::placeholders::_2, 3, dosk::placeholders::_1, 5);
 std::cout << binder(4, 2, 0) << std::endl;
 return 0;
}

以上是“C++ STL内 std::{bind/tuple/function}的使用方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


网页标题:C++STL内std::{bind/tuple/function}的使用方法
路径分享:http://cdkjz.cn/article/pgcgsd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220