对于通过引用接受参数的方法,使用bind1st [英] Using bind1st for a method that takes argument by reference
本文介绍了对于通过引用接受参数的方法,使用bind1st的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的结构:
struct A {
void i(int i) {}
void s(string const &s) {}
};
现在,当我尝试这样:
bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");
第一行编译成功,但第二行产生错误:
The first line compiles OK, but the second generates an error:
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 &>
]
可能有什么问题?如何解决?
What could be the problem? How could I fix it?
修改:
参数是一个问题。因此,如果我将 i
方法更改为 void i(int& i){}
It seems that any reference argument is a problem. So if I change the i
method to void i(int &i) {}
I get a similar error.
推荐答案
std :: bind1st和std :: bind2nd不接受带引用参数的函数,这些论点。您可以
std::bind1st and std::bind2nd don't accept functors which take reference arguments, because they themselves form references to these arguments. You can
- 使用指针代替函数输入
- 使用boost :: bind
- 接受复制字符串的性能成本
这篇关于对于通过引用接受参数的方法,使用bind1st的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文