David Alex David Alex - 1 month ago 15
C++ Question

Functionality of void operator()()

I am confused about the functionality of

void operator()()

Could you tell me about that, for instance:

class background_task

void operator()() const

background_task f;

std::thread my_thread(f);

Here, why we need
? What is the meaning of the first and second
? Actually, I know the operation of normal operator, but this operator is confusing.


The first () is the name of the operator - it's the operator that is invoked when you use () on the object. The second () is for the parameters, of which there are none.

Here's an example of how you would use it:

background_task task;
task();  // calls background_task::operator()