15

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.

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
  • Given that we are in 2011 and have C++11 you might want to have a look at C++11s `std::bind` or `boost::bind` which make those things orders of magnitude easier to handle. – PlasmaHH Oct 19 '11 at 14:18
  • 2
    @PlasmaHH: Unfortunatelly, I cannot use C++11 in this project. – Juraj Blaho Oct 19 '11 at 14:19

4 Answers4

11

std::bind1st and std::bind2nd don't accept functors which take reference arguments, because they themselves form references to these arguments. You can

  1. use pointers for your function inputs instead of references
  2. use boost::bind
  3. accept the performance cost of copying the string
thiton
  • 35,651
  • 4
  • 70
  • 100
6

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

C++03 lacked the facilities to build a perfect bind library. This issue is fixed in C++11 with perfect forwarding and std::bind.

bames53
  • 86,085
  • 15
  • 179
  • 244
0

Look at this post were the requirements of template parameters are explained. As you already assumed the reference to the std::string is the problem. It's not a valid template parameter.

Community
  • 1
  • 1
codencandy
  • 1,701
  • 1
  • 10
  • 20
  • 1
    While this is true, the argument type of the function is transported from mem_fun to bind1st via the first_argument_type typedef, and a reference is a valid typedef. So std::bind1st *could* theoretically accept references, but is implemented differently (and this implementation is mandated by the standard, IIRC). – thiton Oct 19 '11 at 14:42
0

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);
}
Tio Pepe
  • 3,071
  • 1
  • 17
  • 22