其他分享
首页 > 其他分享> > c – 如何指定callable应该指向的重载?

c – 如何指定callable应该指向的重载?

作者:互联网

参见英文答案 > How do I specify a pointer to an overloaded function?                                    6个

#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();
}

此代码无法编译,因为& Callable :: start不明确.有没有办法指定应该在std :: thread构造函数中使用哪个重载?

解决方法:

你可以施放:

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

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

标签:c,overload-resolution
来源: https://codeday.me/bug/20190725/1531531.html