What is a callable object in C++?

北慕城南 提交于 2019-12-03 05:55:41

A callable object is something that can be called like a function, with the syntax object() or object(args); that is, a function pointer, or an object of a class type that overloads operator().

The overload of operator() in your class makes it callable.

There are two steps here. In the C++ Standard, a "function object" is an object that can appear on the left-hand side of a parenthesized argument list, i.e, a pointer to function or an object whose type has one or more operator()s. The term "callable object" is broader: it also includes pointers to members (which can't be called with the normal function call syntax). Callable objects are the things that can be passed to std::bind etc. See 20.8.1 [func.def] and 20.8[function.objects]/1.

An object which has at least an overloaded operator() is a callable object, and that operator plus its object can be invoked like function invoking:

CallableClass obj;
obj();

A callable object is an object instance from a class with operator() overloaded:

struct Functor {
    ret_t operator()();
    // ...
}

Functor func;  // func is a callable object

or a dereferenced-function pointer:

ret_t func() {
   // ...
}

func;  // func converts to a callable object
Carlo Wood

Since C++17, a Callable object is actually defined by the standard; see https://en.cppreference.com/w/cpp/named_req/Callable for details.

In C++11, a callable element can be either:

  • A function pointer,
  • A member function pointer, (it is different from the previous one, check here)
  • An object of a functor class (a class wherein its operator() is implemented),
  • Anonymous functions (Lambdas),
  • Or any of the mentioned elements wrapped in a std::function object.

This means that you can use each of the mentioned callable elements to launch a std::thread. Take a look at the following sample code:

std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

int func()
{
   return std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
}

class A
{
public:
   int mem_func() 
   { 
      return std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
   }
};

class B
{
public:
   int operator()()
   {
      return std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
   }
};

auto lambda = []() { return std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>()); };


void main()
{
   A a;
   B b;

   std::function<int()> f1 = &func;
   std::function<int()> f2 = std::bind(&A::mem_func, &a);
   std::function<int()> f3 = std::bind(&B::operator(), &b);
   std::function<int()> f4 = lambda;

   std::thread t1 = std::thread(func);
   std::thread t2 = std::thread(&A::mem_func, a);
   std::thread t3 = std::thread(&B::operator(), b);
   std::thread t4 = std::thread(lambda);

   std::thread t5 = std::thread(f1);
   std::thread t6 = std::thread(f2);
   std::thread t7 = std::thread(f3);
   std::thread t8 = std::thread(f4);

   t1.join();
   t2.join();
   t3.join();
   t4.join();
   t5.join();
   t6.join();
   t7.join();
   t8.join();
}

Function object add member function pointers yields what are known as callable objects. When we in c++ 98/03,we use the class override the operator() as function.In general, we call that class function.It has the advantage of storing state of the function and other function can't.So it is the most important concept.And border, we call this style class function and other c style function and pointer to c style function "function object". The callable object is just "callable" object. It include function object and member function pointers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!