Passing static method as argument, no address-of operator required?

 ̄綄美尐妖づ 提交于 2019-12-10 19:07:13

问题


class ThreadWorker
{
public:
    ThreadWorker(void);
    virtual ~ThreadWorker(void);

    static void DoSomething();
};


int main()
{
    boost::thread thread1(ThreadWorker::DoSomething);
    boost::thread thread2(ThreadWorker::DoSomething);
    boost::thread thread3(&ThreadWorker::DoSomething);
}

I'm playing around with Boost.Thread and I notice it doesn't seem to matter whether I use the address of operator (&) or not when passing a static member function as an argument. Does it not matter? And if not, why? Is one way more correct than the other?


回答1:


It effectively does not matter. Functions (free functions and static member functions, not non-static member functions) decay to function pointers. No way is more correct than the other, I happen to prefer the explicit one though.

C++11 Standard, 4.3/1:

An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.

C++11 Standard, 5.2.2/1 - Function call:

There are two kinds of function call: ordinary function call and member function call. A static member function is an ordinary function.



来源:https://stackoverflow.com/questions/7761527/passing-static-method-as-argument-no-address-of-operator-required

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