Search code examples
c++overload-resolution

How to specify the overload a callable should point to?


#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 &Callable::start is ambiguous. Is there a way to specify which overload should be used in std::thread constructor?


Solution

  • You can cast:

    using callback_type = void (Callable::*)();
    
    std::thread some_thread(
        static_cast<callback_type>(&Callable::start), &some_object );
    //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^