[英]Using bind1st for a method that takes argument by reference
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. 因此,如果我将
i
方法更改为void i(int &i) {}
我会收到类似的错误。
std::bind1st and std::bind2nd don't accept functors which take reference arguments, because they themselves form references to these arguments. std :: bind1st和std :: bind2nd不接受带引用参数的仿函数,因为它们自己形成对这些参数的引用。 You can
您可以
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 看一下针对gcc的bug报告以及由此产生的讨论: Bug 37811 - 带有引用参数的mem_fun上的bind1st失败
C++03 lacked the facilities to build a perfect bind library. C ++ 03缺乏构建完美绑定库的功能。 This issue is fixed in C++11 with perfect forwarding and std::bind.
这个问题在C ++ 11中修复,具有完美转发和std :: bind。
Change references by pointers 通过指针更改引用
#include <functional>
#include <string>
struct A {
void i(int i) {}
void s(const std::string *s) {}
};
int main(void)
{
A a;
std::bind1st(std::mem_fun(&A::i), &a)(0);
const std::string s("");
std::bind1st(std::mem_fun(&A::s), &a)(&s);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.