我有这样的结构:
struct A {
void i(int i) {}
void s(string const &s) {}
};
Run Code Online (Sandbox Code Playgroud)
现在当我尝试这个:
bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");
Run Code Online (Sandbox Code Playgroud)
第一行编译好,但第二行生成错误:
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 &>
]
Run Code Online (Sandbox Code Playgroud)
可能是什么问题呢?我该怎么办呢?
编辑:
似乎任何引用参数都是一个问题.所以,如果我改变i方法,void i(int &i) {}我会得到类似的错误.
thi*_*ton 11
std :: bind1st和std :: bind2nd不接受带引用参数的仿函数,因为它们自己形成对这些参数的引用.您可以
问题是库规范中的缺陷.
看一下针对gcc的bug报告以及由此产生的讨论:Bug 37811 - 带有引用参数的mem_fun上的bind1st失败
C++ 03缺乏构建完美绑定库的功能.这个问题在C++ 11中修复,具有完美转发和std :: bind.