c++ - std::function which returns any summable type? -


my graph data structure parallel algorithms has following iterator method:

/**  * iterate in parallel on nodes of graph , call handler (lambda closure).  */ void graph::parallelfornodes(std::function<void(node)> handle) {     #pragma omp parallel     (node v = 0; v < z; ++v) {         // call here         handle(v);     } } 

i have declared handle function template parameter, thought preferred way in c++11 use std::function.

now want perform parallel reduction using openmp such iterator. return values of each call handle reduced sum. function templates, looks like:

template<typename l> inline double graph::parallelsumfornodes(l handle) {     double sum = 0.0; #pragma omp parallel reduction(+:sum)     (node v = 0; v < z; ++v) {         // call here         if (exists[v]) {             sum += handle(v);         }     }     return sum; } 

what equivalent using std::function? can define type of handle return double or int (because body of function works both).

perhaps 2 parameter member function, along lines of std::accumulate?

template<typename handle, typename accumulator> accumulator graph::parallelsumfornodes(handle handle, accumulator sum)  { #pragma omp parallel reduction(+:sum)     (node v = 0; v < z; ++v) {         // call here         if (exists[v]) {             sum += handle(v);         }     }     return sum; } 

note has same caveat std::accumulate: have careful type of accumulator pass it.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -