#include <thread> struct Callable { void start() {}; void start(unsigned) {}; }; int main() { Callable some_object; std::thread some_thread( &Callable::start, &some_object ); some_thread.join(); }
This code does not compile because it is &Callable::startambiguous. Is there a way to indicate which overload should be used in the constructor std::thread?
&Callable::start
std::thread
You can use:
using callback_type = void (Callable::*)(); std::thread some_thread( static_cast<callback_type>(&Callable::start), &some_object ); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can define a pointer to a member and use it in a function call
void ( Callable::*p )( unsigned ) = &Callable::start; std::thread some_thread( p, &some_object );
:
typedef void (Callable::*voidFunc)(); int main() { Callable some_object; std::thread some_thread( (voidFunc)&Callable::start, &some_object );
, :
void (Callable::*)() start_fun = &Callable::start; std::thread some_thread( start_fun, &some_object );
, , - .