将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)("");
第一行编译正常,但第二行生成错误:
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 &>
]
可能是什么问题?我该如何解决它?
编辑:
似乎任何参考参数都是一个问题。因此,如果我将 i
方法更改为 void i(int &i) {}
我会收到类似的错误。
I have a struct like this:
struct A {
void i(int i) {}
void s(string const &s) {}
};
Now when I try this:
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?
Edit:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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
该问题是库规范中的缺陷。
查看针对 gcc 的错误报告以及由此产生的讨论: Bug 37811 - bind1st 失败带有引用参数的 mem_fun
C++03 缺乏构建完美绑定库的工具。此问题已在 C++11 中通过完美转发和 std::bind 修复。
The issue is a defect in the library specification.
Take a look at this bug report against gcc and the resulting discussion: Bug 37811 - bind1st fails on mem_fun with reference argument
C++03 lacked the facilities to build a perfect bind library. This issue is fixed in C++11 with perfect forwarding and std::bind.
看看这篇帖子,其中解释了模板参数的要求。
正如您已经假设对 std::string 的引用是问题所在。它不是有效的模板参数。
Look at this post were the requirements of template parameters are explained.
As you already assumed the reference to the std::string is the problem. It's not a valid template parameter.
通过指针更改引用
Change references by pointers