Saturday, 26. August 2017 08:40PM
#Callable Objecgt(可被调用的对象)
Callable Object可以是

  • 函数
  • 指向成员函数的指针
  • 函数对象
  • lambda,严格来说也是函数对象

Example:
~~~
void func (int x, int y);

auto l = [] () {

};

class C {
public:
void operator () (int x, int y) const;
void memfunc (int x, int y) const;
};

int main()
{
C c;
std::shared_ptr sp(new C);

//bind() uses callable objects to bind arguments:
std::bind(func,77,33)();
std::bind(l,77,33)();
std::bind(C(),77,33)();
std::bind(&C::memfunc,c,77,33)();
std::bind(&C::memfunc,sp,77,33)();

//async() uses callable objects to start(background) tasks:
std::async(func,42,77);
std::async(l,42,77);
std::async(c,42,77);
std::async(&C::memfunc,&c,42,77);
std::async(&C::memfunc,sp,42,77); } ~~~

如果想声明callable object, 一般而言可使用class std::function<>