10.13. C++ Reference in Data-Sharing Clauses#

C++ reference types are allowed in data-sharing attribute clauses as of OpenMP 4.5, except for the threadprivate, copyin and copyprivate clauses. (See the Data-Sharing Attribute Clauses Section of the 4.5 OpenMP specification.) When a variable with C++ reference type is privatized, the object the reference refers to is privatized in addition to the reference itself. The following example shows the use of reference types in data-sharing clauses in the usual way. Additionally it shows how the data-sharing of formal arguments with a C++ reference type on an orphaned task generating construct is determined implicitly. (See the Data-sharing Attribute Rules for Variables Referenced in a Construct Section of the 4.5 OpenMP specification.)

//%compiler: clang
//%cflags: -fopenmp

/*
* name: cpp_reference.1
* type: C++
* version: omp_4.5
*/

void task_body (int &);
void gen_task (int &x) { // on orphaned task construct reference argument
  #pragma omp task // x is implicitly determined firstprivate(x)
  task_body (x);
}
void test (int &y, int &z) {
  #pragma omp parallel private(y)
  {
    y = z + 2;
    gen_task (y); // no matter if the argument is determined private
    gen_task (z); // or shared in the enclosing context.

    y++;          // each thread has its own int object y refers to
    gen_task (y);
  }
}