3
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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
User
  • 62,498
  • 72
  • 186
  • 247
  • What compiler are you using? The ones that I use these days complain to me when not using `&`, though older compilers were totally fine with it. – Jim Buck Oct 13 '11 at 23:29

1 Answers1

7

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.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • I'm confused about this sentence: "Functions (non-member functions that is) decay to function pointers". I'm talking about *member* functions, aren't I? – User Oct 13 '11 at 23:29
  • @User: A static function is not a member function. – K-ballo Oct 13 '11 at 23:29
  • Ok I believe you, but aren't they called "static member functions"? Are you saying "static member functions" are not "member functions"? – User Oct 13 '11 at 23:32