c++function-pointersstatic-methodsmember-functionsaddress-operator

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


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?


Solution

  • 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.