views:

197

answers:

1

I have some code that 100% works for the use case I have. I'm just wondering if anyone can explain how and why it works.

I have a template class that sits between some code that handles threading and network communication and the library user to pass data received from the server to the user.

template <class Bar,
          class Baz,
          class BazReturnType,
          void (Bar::*BarSetterFunction)(const BazReturnType &),
          BazReturnType (Baz::*BazGetterFunction)(void) const>
class Foo
{
    Foo( Bar *bar )
        : m_bar(bar)
    {
    }

    void FooMemberFunction( const Baz *baz )
    {
        boost::bind( BarSetterFunction, m_bar,
                     boost::bind( BazGetterFunction, baz )() ) ();
    }

    Bar *m_bar;
};

This template is instantiated and used in the library depending on the types of Bar and Baz like so:

typedef Foo<MyBar,
            MyBaz,
            ReturnTypeFromBazGetterFunction,
            &MyBar::ActualSetterFunction,
            &MyBaz::ActualGetterFunction >
    MyFoo;

MyBar *bar = new MyBar;
MyBaz *baz = new MyBaz;
MyFoo *f = new MyFoo( bar );
f->FooMemberFunction( baz );

This all works and boost::bind calls the getter/setter functions to pass the data around where it needs to go. How and why does passing pointers to member functions as a template argument like in this case work?


In response to comments, I hadn't realized that pointers to member functions were valid template arguments. It's not something I had seen "in the wild" before. I tried it and it worked, but I wasn't expecting it to.

+1  A: 

When you ask a question along the lines of "why something works?", it implies that the fact that it works is somehow surprising to you. It is impossible to answer the question unless you explain why you find it surprising.

Why it works? Because the language specification explicitly says that it shall work. There's no other answer, until you explain your concerns in more detail.

AndreyT
I guess "because the language specification says so" is the answer. I hadn't realized that pointers to member functions were valid template arguments according to the standard.
Grant Limberg