How to indicate the overload of the called should indicate?

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

+3
source share
4 answers

You can use:

using callback_type = void (Callable::*)();

std::thread some_thread(
    static_cast<callback_type>(&Callable::start), &some_object );
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+4
source

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 );
+2
source

:

typedef void (Callable::*voidFunc)();

int main()
{   
    Callable some_object;
    std::thread some_thread( (voidFunc)&Callable::start, &some_object );
+1

, :

void (Callable::*)() start_fun = &Callable::start;
std::thread some_thread( start_fun, &some_object );

, , - .

+1

All Articles