Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the overload a callable should point to? [duplicate]

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

like image 977
qdii Avatar asked Dec 12 '25 03:12

qdii


1 Answers

You can cast:

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

std::thread some_thread(
    static_cast<callback_type>(&Callable::start), &some_object );
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
like image 74
David G Avatar answered Dec 13 '25 17:12

David G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!