Cannot use std :: ptr_fun with function that is being referenced
Not allowed by C ++ 11 or Boost.
I am trying to get the following code to compile but I am having problems. std::ptr_fun
doesn't seem like parameters that are references.
#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 is already defined or declared
But if I change the parameter in Function
to T x
, it works!
Is there a way to do this conveniently without changing Function
?
Real-time examples:
source to share
You cannot do this. This is a fundamental limitation for std :: bind1st and std :: bind2nd. The problem is that it defines two () operators and one of them already has a const and on it. Thus, the compiler sees two identical functions. It will not be fixed as C ++ 11 has already deprecated these methods.
See also:
Using bind1st for a method that takes an argument by reference
source to share