binding a member function with an argument of reference type

Discussion in 'C++' started by Triple-DES, Oct 16, 2009.

  1. Triple-DES

    Triple-DES Guest

    Submitted for your consideration:

    #include <functional>

    struct C { void f(const int&) {} };

    template <typename F>
    void call(F f)
    {
    f(123);
    }
    int main()
    {
    using namespace std;
    C c;
    call ( bind1st(mem_fun( &C::f), &c) );
    }

    This seemingly fails on most implementations because the library
    attempts to form a reference to a reference. Is it possible to bind
    this function using only the C++03 standard library utilities?
     
    Triple-DES, Oct 16, 2009
    #1
    1. Advertising

  2. On 16/10/09 14:24, Triple-DES wrote:
    > Submitted for your consideration:
    >
    > #include<functional>
    >
    > struct C { void f(const int&) {} };
    >
    > template<typename F>
    > void call(F f)
    > {
    > f(123);
    > }
    > int main()
    > {
    > using namespace std;
    > C c;
    > call ( bind1st(mem_fun(&C::f),&c) );
    > }
    >
    > This seemingly fails on most implementations because the library
    > attempts to form a reference to a reference. Is it possible to bind
    > this function using only the C++03 standard library utilities?


    Only if you don't use reference arguments.

    Use boost::bind(). It does not have this problem:

    call(boost::bind(&C::f, &c, _1));

    --
    Max
     
    Maxim Yegorushkin, Oct 17, 2009
    #2
    1. Advertising

  3. Triple-DES

    Triple-DES Guest

    On 17 Okt, 17:29, Maxim Yegorushkin <>
    wrote:
    > On 16/10/09 14:24, Triple-DES wrote:
    >
    > > Submitted for your consideration:

    >
    > > #include<functional>

    >
    > > struct C { void f(const int&) {} };

    >
    > > template<typename F>
    > > void call(F f)
    > > {
    > >    f(123);
    > > }
    > > int main()
    > > {
    > >    using namespace std;
    > >    C c;
    > >    call ( bind1st(mem_fun(&C::f),&c) );
    > > }

    >
    > > This seemingly fails on most implementations because the library
    > > attempts to form a reference to a reference. Is it possible to bind
    > > this function using only the C++03 standard library utilities?

    >
    > Only if you don't use reference arguments.
    >
    > Use boost::bind(). It does not have this problem:
    >
    >    call(boost::bind(&C::f, &c, _1));


    Thanks Maxim. Unfortunately, the Boost licensing scheme is
    unacceptable for our application. I ended up writing my own wrapper
    function object. That's roughly 15 lines of (potentially buggy) code,
    that I am only going to use for this very specific task. Not an ideal
    solution.
     
    Triple-DES, Oct 19, 2009
    #3
  4. Triple-DES

    Jeff Flinn Guest

    Triple-DES wrote:
    > On 17 Okt, 17:29, Maxim Yegorushkin <>
    > wrote:
    >> On 16/10/09 14:24, Triple-DES wrote:
    >> Use boost::bind(). It does not have this problem:
    >>
    >> call(boost::bind(&C::f, &c, _1));

    >
    > Thanks Maxim. Unfortunately, the Boost licensing scheme is
    > unacceptable for our application. I ended up writing my own wrapper


    Boost's licensing scheme is probably less encumbering than the license
    of the compiler that you're using. What's problematic with boost's license?

    Jeff
     
    Jeff Flinn, Oct 19, 2009
    #4
  5. Triple-DES

    Triple-DES Guest

    On 19 Okt, 15:24, Jeff Flinn <> wrote:
    > Triple-DES wrote:
    > > On 17 Okt, 17:29, Maxim Yegorushkin <>
    > > wrote:
    > >> On 16/10/09 14:24, Triple-DES wrote:
    > >> Use boost::bind(). It does not have this problem:

    >
    > >>    call(boost::bind(&C::f, &c, _1));

    >
    > > Thanks Maxim. Unfortunately, the Boost licensing scheme is
    > > unacceptable for our application. I ended up writing my own wrapper

    >
    > Boost's licensing scheme is probably less encumbering than the license
    > of the compiler that you're using. What's problematic with boost's license?
    >
    > Jeff


    Nothing as far as I am concerned. I suspect that my organization's
    lavishly paid legal counsel fails to distinguish between the Boost
    Sofware License and the GNU GPL.
     
    Triple-DES, Oct 20, 2009
    #5
    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. Gert  Van den Eynde
    Replies:
    8
    Views:
    650
    Keith H Duggar
    Jan 26, 2004
  2. Reckoner
    Replies:
    11
    Views:
    738
    Steven D'Aprano
    Jan 19, 2009
  3. Dwight Army of Champions

    class data member or member function argument?

    Dwight Army of Champions, Feb 21, 2010, in forum: C++
    Replies:
    4
    Views:
    1,575
    tonydee
    Feb 22, 2010
  4. AikidoGuy
    Replies:
    11
    Views:
    594
    Seebs
    Nov 21, 2011
  5. Ryan Dupuis
    Replies:
    8
    Views:
    1,424
    Eric Sosman
    Oct 2, 2013
Loading...

Share This Page