Passing a pointer to a member function as a template argument. Why does this work?

久未见 提交于 2019-12-09 07:50:40

问题


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:


I think there is a better explanation why it is possible to do so than "because the standard says so":

The reason it works is because pointers-to-members are constant values known at compile time (pointer-to-member is effectively an offset of a member from the start of a class). Thus they can be used as parameters of templates, just as any other integer constant can be.

On the other hand, normal pointers are not compile time constants, because they depend on memory layout which only exists at runtime. They cannot be template arguments.




回答2:


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.



来源:https://stackoverflow.com/questions/1882740/passing-a-pointer-to-a-member-function-as-a-template-argument-why-does-this-wor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!