c++ - OpenMP reduction with template type -


template <typename t, std::size_t n> static t sum(const std::array<t, n>& a) {     t result;      // type of result (t) not determined when pre-process?     #pragma omp parallel reduction(+: result)     for(int = 0; < static_cast<int>(n); i++)     {         result += a[i];     }     return result; } 

i can compile , run above code msvc , gcc. yes, it's excellent!

but question in code comment; "because type of result (t) not determined while pre-processing '#pragma', how compiler validate type of result suited openmp reduction?".

i'm sure it's ok if t=double , ng if t=std::string, how pre-processor know type of t?

i remember couldn't compile above code minor c++ compiler long time ago.

let me ask behavior (compilable or uncompilable) correct in context of c++/openmp specifications.

it's unspecified (for openmp 3.0 or later) or undefined (for openmp 2.5)

reduction 1 of data-sharing attribute clauses, , openmp application program interface version 2.5 says:

2.8.3 data-sharing attribute clauses
---- c/c++ ----
if variable referenced in data-sharing attribute clause has type derived template, , there no other references variable in program, behavior related variable undefined.
---- c/c++ ----

openmp application program interface version 3.0 says:

2.9.3 data-sharing attribute clauses
---- c/c++ ----
if variable referenced in data-sharing attribute clause has type derived template, , there no other references variable in program, behavior related variable unspecified.
---- c/c++ ----


Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -