std::bind2nd and non-const references

Discussion in 'C++' started by red floyd, Dec 29, 2003.

  1. red floyd

    red floyd Guest

    Both gcc 3.3.1 and MSVC 7.1 complain about the second for_each() call
    in the following code. Why can't I use bind2nd for a functor with a non-const
    reference as the second parameter? I have to use kludge it by using something like
    KludgeFunctor().

    ---------------------------
    #include <vector>
    #include <algorithm>
    #include <functional>

    class A {
    public:
    A() { }
    A(const A&) { }
    };

    class B {
    public:
    B() { }
    B(const B&) { }
    };

    class GoodFunctor : public std::binary_function<A, B, void>
    {
    public:
    void operator()(const A&, const B&) const { }
    };

    class BadFunctor : public std::binary_function<A, B, void>
    {
    public:
    void operator()(const A&, B&) const { }
    };


    class KludgeFunctor : public std::unary_function<A, void>
    {
    B& b;
    public:
    KludgeFunctor(B& b_) : b(b_) { }
    void operator()(const A&) const { }
    };

    void f()
    {
    std::vector<A> v;
    B b;

    // this compiles
    std::for_each(v.begin(),
    v.end(),
    std::bind2nd(GoodFunctor(), b));

    // this generates an error
    std::for_each(v.begin(),
    v.end(),
    std::bind2nd(BadFunctor(), b));

    // this is the workaround for BadFunctor
    std::for_each(v.begin(),
    v.end(),
    KludgeFunctor(b));
    }
    ----------------------------------
    red floyd, Dec 29, 2003
    #1
    1. Advertising

  2. red floyd

    Jeff Schwab Guest

    red floyd wrote:
    > Both gcc 3.3.1 and MSVC 7.1 complain about the second for_each() call
    > in the following code. Why can't I use bind2nd for a functor with a
    > non-const
    > reference as the second parameter? I have to use kludge it by using
    > something like
    > KludgeFunctor().
    >
    > ---------------------------
    > #include <vector>
    > #include <algorithm>
    > #include <functional>
    >
    > class A {
    > public:
    > A() { }
    > A(const A&) { }
    > };
    >
    > class B {
    > public:
    > B() { }
    > B(const B&) { }
    > };
    >
    > class GoodFunctor : public std::binary_function<A, B, void>
    > {
    > public:
    > void operator()(const A&, const B&) const { }
    > };
    >
    > class BadFunctor : public std::binary_function<A, B, void>
    > {
    > public:
    > void operator()(const A&, B&) const { }
    > };
    >
    >
    > class KludgeFunctor : public std::unary_function<A, void>
    > {
    > B& b;
    > public:
    > KludgeFunctor(B& b_) : b(b_) { }
    > void operator()(const A&) const { }
    > };
    >
    > void f()
    > {
    > std::vector<A> v;
    > B b;
    >
    > // this compiles
    > std::for_each(v.begin(),
    > v.end(),
    > std::bind2nd(GoodFunctor(), b));
    >
    > // this generates an error
    > std::for_each(v.begin(),
    > v.end(),
    > std::bind2nd(BadFunctor(), b));
    >
    > // this is the workaround for BadFunctor
    > std::for_each(v.begin(),
    > v.end(),
    > KludgeFunctor(b));
    > }
    > ----------------------------------


    std::bind2nd takes a const reference as its second argument; see the
    following code from the gcc stl_function.h header. It shouldn't be
    difficult to roll your own version of bind2nd to use a non-const reference.

    template <class _Operation, class _Tp>
    inline binder2nd<_Operation>
    bind2nd(const _Operation& __fn, const _Tp& __x)
    {
    typedef typename _Operation::second_argument_type _Arg2_type;
    return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    }
    Jeff Schwab, Dec 29, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dzikus
    Replies:
    4
    Views:
    3,455
    Mark Stijnman
    Apr 23, 2005
  2. Noah Roberts

    bind2nd, mem_fun, and references

    Noah Roberts, Jul 15, 2006, in forum: C++
    Replies:
    8
    Views:
    787
    P.J. Plauger
    Jul 15, 2006
  3. Javier
    Replies:
    2
    Views:
    532
    James Kanze
    Sep 4, 2007
  4. Replies:
    4
    Views:
    2,609
  5. K. Frank
    Replies:
    4
    Views:
    170
Loading...

Share This Page