std::bind and overloaded function

后端 未结 1 2005
终归单人心
终归单人心 2021-02-19 05:03

Please refer the following code snippet. I want to use the std::bind for overloaded function foobar. It calls only the method with no arguments.

<
相关标签:
1条回答
  • 2021-02-19 05:41

    You need to use placeholders for the unbound arguments:

    auto a2 = std::bind(static_cast<void(Client::*)(int)>(&Client::foobar), cl,
                        std::placeholders::_1);
    a2(5);
    

    You can also perform the binding with a lambda capture (note that this is binds cl by reference, not by value):

    auto a2 = [&](int i) { cl.foobar(i); };
    
    0 讨论(0)
提交回复
热议问题