Vui lòng tham khảo đoạn mã sau. Tôi muốn sử dụng std::bind
cho chức năng quá tải foobar
. Nó chỉ gọi phương thức mà không có đối số.std :: hàm ràng buộc và quá tải
#include <functional>
#include <iostream>
class Client
{
public :
void foobar(){std::cout << "no argument" << std::endl;}
void foobar(int){std::cout << "int argument" << std::endl;}
void foobar(double){std::cout << "double argument" << std::endl;}
};
int main()
{
Client cl;
//! This works
auto a1 = std::bind(static_cast<void(Client::*)(void)>(&Client::foobar),cl);
a1();
//! This does not
auto a2= [&](int)
{
std::bind(static_cast<void(Client::*)(int)>(&Client::foobar),cl);
};
a2(5);
return 0;
}
Bạn đang thiếu 'return' trong lambda của mình. – ildjarn