关于 Bind1st 和 C++ 中的 men_fun 的老问题; STL
一些简单的代码将演示这个问题:
class Foo {};
struct Bar {
bool foo(const Foo &f) const { return false; }
};
int main() {
Bar bar;
vector<Foo> v;
std::find_if(v.begin(), v.end(), std::bind1st(
std::mem_fun_ref(&Bar::foo), bar));
return 0;
}
现在,对于这段代码,VS2010 c++ 编译器会抱怨: 错误 C2535: bool std::binder1st<_Fn2>::operator()(const Foo&) const: 成员函数已定义或声明
在早期版本的 Visual Studio 上,还会出现两个与引用问题相关的编译错误。虽然这些问题在 VS2010 中已经消失,但 C2535 仍然存在。
Some simple code will demonstrate the problem:
class Foo {};
struct Bar {
bool foo(const Foo &f) const { return false; }
};
int main() {
Bar bar;
vector<Foo> v;
std::find_if(v.begin(), v.end(), std::bind1st(
std::mem_fun_ref(&Bar::foo), bar));
return 0;
}
Now, for this code, the VS2010 c++ compiler will complain:
error C2535: bool std::binder1st<_Fn2>::operator()(const Foo&) const:
member function already defined of declared
On earlier versions of Visual Studio, there would be two more compilation errors which are related to reference to reference issues. While these problems have gone in VS2010, the C2535 remains.
This question is similar to this one. As suggested by that post, i can use std::bind, or the boost library as alternatives. They work fine, but for now, i'd like to know is it possible to use the old bind1st style in this case, or is this problem more of a defect inherent in the STL functional framework?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

这不是VS或任何编译器的问题。
mem_fun_ref
返回的类型是mem_fun_ref_t
,它继承自一元函数。该函子采用一个参数,该参数必须是成员函数所属的类类型。mem_fun_ref
不能用于带有参数的成员函数。活页夹已被弃用是有原因的:它们很糟糕。
This is not an issue of VS or any compiler. The type returned by
mem_fun_ref
ismem_fun_ref_t
which inherits from unary function. This functor takes one argument which must be of the class type the member function belongs to.mem_fun_ref
cannot work for member functions that take arguments.The binders have been deprecated for a reason: They suck.