The documentation on the cppreference shows some interesting things: To begin with the definition of a simple function and a simple structure, they are necessary for an example, like lemmas in theorems:
struct Foo { Foo(int num) : num_(num) {} void print_add(int i) const { std::cout << num_+i << '\n'; } int num_; }; void print_num(int i) { std::cout << i << '\n'; } Next is the announcement:
std::function<void(int)> f_display = print_num; I am interested in the template parameter, I thought: "is this the type of function?", Because I have never seen parentheses anywhere in the template parameter.
But then comes another interesting thing:
std::function<void(const Foo&, int)> f_add_display = &Foo::print_add; the Foo::print_add takes an int as input, which is a template parameter
What type is it? Those. What is the general name for these types? There it is not written. And I can not google it. I understand that this is a target object, it contains a function call (a pointer to a method and an argument), but what role do the parentheses play here? I can not just be based on logic, here you need to read.
It is not necessary for me to chew, it will be enough to indicate that google to know more about this, or just to give a link to the necessary literature.
std::functionis a more convenient version of old function pointers.std::function<void(int)> f_display = print_num;void(int)- the function returns nothing, takes an integer.std::function<void(const Foo&, int)> f_add_display = &Foo::print_addthis function is non-static inside the structure, to call it, you need an instance of the structure, we create a pointer to the function that accepts this structure and makes a call. roughly speakingvoid f_add_display(const Foo& p1, int p2){ return p1.print_add(p2);}we have a pointer to this function. There's more about lambda .. - pavelstd::functioncan store pointers to member functions. Excellent designstd::functionby the committee. - ixSci