模拟TR1中bind的实现

最近几天都在看模板,它真的可以算是C++中最复杂的部分了。昨天和今天又看了一下TR1中functional的部分,发现其中的bind真可谓是无比强大。源代码还没有完全看完,不过已经忍不住自己模拟着写了一个,与TR1中的bind相比,缺少了占位符的功能,虽说这样一来彪悍程度大打折扣,不过花了这两个多小时实现了bind的其它部分,还是有些成就感的,哈哈,自己发文YY一下

 

  1. #include <iostream>
  2. using namespace std;
  3. template <typename Signature>
  4. class BindZ;
  5. template <typename Signature>
  6. class result_ofZ;
  7. template <typename Signature>
  8. class get_raw_typeZ;
  9. template <typename R, typename T>
  10. class result_ofZ<R (T)>
  11. {
  12. public:
  13.     typedef R type;
  14. };
  15. template <typename T>
  16. class get_raw_typeZ<T*>
  17. {
  18. public:
  19.     typedef T raw_type;
  20. };
  21. template <typename F, typename T>
  22. class BindZ<F (T)>
  23. {
  24. public:
  25.     BindZ(F f, T a) : m_fun(f), m_arg(a)
  26.     {
  27.     }
  28.     typename result_ofZ<typename get_raw_typeZ<F>::raw_type>::type operator ()()
  29.     {
  30.         return m_fun(m_arg);
  31.     }
  32. private:
  33.     F m_fun;
  34.     T m_arg;
  35. };
  36. template <typename F, typename T>
  37. BindZ<F (T)> bindZ(F f, T a)
  38. {
  39.     return BindZ<F (T)>(f, a);
  40. }
  41. int foo(int a)
  42. {
  43.     cout << a << endl;
  44.     return 0;
  45. }
  46. int main()
  47. {
  48.     cout << typeid(bindZ(foo, 1)()).name() << endl;
  49.     return 0;
  50. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值