3

No C++11 or Boost allowed.

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.

#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

But if I change the parameter in Function to T x it works!

Is there any way to get this working conveniently without modifying Function?

Live examples:

http://ideone.com/Eno7gF

http://ideone.com/kGmv7r

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • VS2013 supports a large portion of the C++11 library, so despite your restriction you do have access to `std::bind` and `std::placeholders` – Mgetz Dec 03 '14 at 18:50

1 Answers1

2

You cannot do this. It is a fundamental limitation to std::bind1st and std::bind2nd. The problem is that it defines two () operators, and one of them already has const & on it. So the compiler sees two identical functions. It won't be fixed since C++11 has already deprecated these methods.

See also:

Using bind1st for a method that takes argument by reference

weird compiler error using bind2nd(): "member function already defined or declared" instead of "reference to reference"

Bind2nd issue with user-defined class

Community
  • 1
  • 1
Moby Disk
  • 3,761
  • 1
  • 19
  • 38