0

The return values of bind1st and bind2nd are derived from unary_function. By calling them, I think they provide a function object that accepts one argument. But this maybe is wrong.

Here is my code.

template<typename _T>
class fun: public std::unary_function<_T, _T>
{
public:
    _T operator()(_T arg1, _T arg2) const {return arg1 + arg2;}
};

int main() {
    bind2nd(fun<int>(),10)(10); //My intention is to directly call the results of bind2nd
}

A lot of build errors occur. Why is this wrong?

richard.g
  • 3,585
  • 4
  • 16
  • 26
  • 1
    Note that `_T` is a reserved identifier! – Dietmar Kühl Dec 12 '13 at 15:31
  • "A lot of build errors occur." Well, in general the first error lines are really helpful. You should include them in your question. Or at least describe what is written in the error message... – Stephane Rolland Dec 12 '13 at 15:38
  • Do you feel the difference between unary and binary function? `bind2nd` requires the last one. – Constructor Dec 12 '13 at 15:42
  • 1
    Note that this is all deprecated in C++11. Use `bind` instead of `bind1st` and `bind2nd`, and don't bother deriving from `unary_function` or `binary_function`. – Mike Seymour Dec 12 '13 at 16:12

1 Answers1

3

I believe a unary function operates on one parameter, while a binary function operates on two. For example

  T operator()(T arg1, T arg2) const {return arg1 + arg2;}

is a binary_function.

Change the template (and consider not using leading underscroes):

template<typename T>
class fun: public std::binary_function<T, T, T>
//                     ^^^^^^--- well, it takes two parameters
{
public:
    T operator()(T arg1, T arg2) const {return arg1 + arg2;}
};

So, fun is a binary functor. After you bind one of its arguments, e.g. by calling std::bind2nd(func<int>(),10) you will then have a unary function. This will not alter the type of the input to the bind2nd call.

doctorlove
  • 18,872
  • 2
  • 46
  • 62