[英]Cannot use std::ptr_fun with function that takes reference
No C++11 or Boost allowed. 不允许使用C ++ 11或Boost。
I am trying to get the following code to compile but I'm having problems. 我试图让以下代码编译,但我遇到了问题。
std::ptr_fun
doesn't seem to like parameters that are references. std::ptr_fun
似乎不喜欢引用的参数。
#include <algorithm>
#include <functional>
#include <vector>
struct Something
{
};
template<class T>
T Function(const T& x, int s)
{
// blah blah
return x;
}
int main()
{
std::vector<Something> data(20);
std::transform(data.begin(), data.end(), data.begin(), std::bind2nd(std::ptr_fun(Function<Something>), 8));
}
VS2013 error message: error C2535: 'Something std::binder2nd>::operator ()(const Something &) const' : member function already defined or declared VS2013错误消息:错误C2535:'Something std :: binder2nd> :: operator()(const Something&)const':已定义或声明的成员函数
But if I change the parameter in Function
to T x
it works! 但是如果我将
Function
的参数更改为T x
就可以了!
Is there any way to get this working conveniently without modifying Function
? 有没有办法在不修改
Function
情况下方便地工作?
Live examples: 实例:
You cannot do this. 你不能做这个。 It is a fundamental limitation to std::bind1st and std::bind2nd.
这是std :: bind1st和std :: bind2nd的基本限制。 The problem is that it defines two () operators, and one of them already has const & on it.
问题是它定义了两个()运算符,其中一个已经有const&。 So the compiler sees two identical functions.
所以编译器看到两个相同的函数。 It won't be fixed since C++11 has already deprecated these methods.
它不会被修复,因为C ++ 11已经弃用了这些方法。
See also: 也可以看看:
Using bind1st for a method that takes argument by reference 将bind1st用于通过引用获取参数的方法
weird compiler error using bind2nd(): "member function already defined or declared" instead of "reference to reference" 使用bind2nd()的奇怪的编译器错误:“已定义或声明的成员函数”而不是“引用引用”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.