mercredi 22 avril 2015

Defining macro improving syntax of specific function

I've created a function declared as:

template <typename Container, typename Task>
void parallel_for_each(Container &container, Task task,
                       unsigned number_of_threads = std::thread::hardware_concurrency())

It's not difficult to guess what it is supposed to do. I'd like to create a macro simplifying the syntax of this function and making the its syntax "loop-like". I've come up with an idea:

#define in ,
#define pforeach(Z,X,Y) parallel_for_each(X,[](Z)->void{Y;})

Where usage as:

pforeach(double &element, vec,
    {
     element *= 2;
    });

works as expected, but this one:

pforeach(double &element in vec,
    {
     element *= 2;
     element /= 2;
    });

gives an error

macro "pforeach" requires 3 arguments, but only 2 given

Do you have any idea how to write a macro allowing even "nicer" syntax? Why "in" doesn't stand for comma in my code?

Aucun commentaire:

Enregistrer un commentaire