将bind1st 用于通过引用获取参数的方法

2024-02-07

我有一个这样的结构:

struct A {
    void i(int i) {}
    void s(string const &s) {}
};

现在当我尝试这个时:

bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");

第一行编译正常,但第二行生成错误:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(299): error C2535: 'void std::binder1st<_Fn2>::operator ()(const std::basic_string<_Elem,_Traits,_Ax> &) const' : member function already defined or declared
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>,
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(293) : see declaration of 'std::binder1st<_Fn2>::operator ()'
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>
          ]
          c:\work\sources\exception\test\exception\main.cpp(33) : see reference to class template instantiation 'std::binder1st<_Fn2>' being compiled
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>
          ]

可能是什么问题呢?我该如何解决它?

Edit:

看来任何参考论证都是有问题的。所以如果我改变i方法void i(int &i) {}我收到类似的错误。


std::bind1st 和 std::bind2nd 不接受采用引用参数的函子,因为它们本身形成对这些参数的引用。你可以

  1. 使用指针作为函数输入而不是引用
  2. 使用升压::绑定
  3. 接受复制字符串的性能成本
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将bind1st 用于通过引用获取参数的方法 的相关文章

随机推荐