Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of constructor with multiple pairs of parentheses

I'm not sure about this piece of code:

someClass(std::list<std::function<void(std::vector<someType>&)>>(&)(const std::vector<someType>&)) {
    ...
}

The constructor for someClass, I believe, takes a reference to a list of functions, each of which will return void and take a reference to a vector of someType. What I'm not sure about is the last pair of parentheses, (const std::vector<someType>&). Is operator() of std::list being overloaded here?

In addition, I'd like to name the std::list parameter, but my initial guess of someClass(std::list<...>(& nameOfList)(...)) did not work, as I cannot access nameOfList.begin(), for example. What would I do here?

Thank you.

like image 674
jmkjaer Avatar asked Jun 01 '19 11:06

jmkjaer


2 Answers

It is a function by itself.

std::list<
         std::function<
            void(std::vector<someType>&)
         >
> (&)(const std::vector<someType>&)

This is a reference to a function that takes as an argument a reference to const std::vector of someType and returns a list of std::functions that take a reference to a std::vector of someType and return void.

Usage example:

#include <vector>
#include <list>
#include <functional>
class someType {};
void func(std::list<std::function<void(std::vector<someType>&)>> (& par)(const std::vector<someType>&)) {
    // some input
    const std::vector<someType> input;
    // the function returns the list
    std::list<std::function<void(std::vector<someType>&)>> res = par(input);
    // we can iterate over the list
    for (auto & i : res) {
        std::vector<someType> other;
        // and call the functions inside
        i(other);
    }
}
like image 81
KamilCuk Avatar answered Nov 15 '22 08:11

KamilCuk


The parameter of the constructor

someClass( std::list<std::function<void(std::vector<someType>&)>>(&)(const std::vector<someType>&)) {

is a reference to a function that has the return type std::list<std::function<void(std::vector<someType>&)>> and one parameter of the type const std::vector<someType>&

like image 26
Vlad from Moscow Avatar answered Nov 15 '22 08:11

Vlad from Moscow