次の方法で共有


コンパイラ エラー C3899

'var': initonly データ メンバーの左辺値は、クラス 'class' の並行領域内で使用することはできません

initonly (C++/CLI) データ メンバーは、並列領域にあるコンストラクターの一部の内部で初期化することができません。 これは、コンパイラがそのコードの内部再配置を行うためです。そのため、実質的にはコンストラクターの一部ではなくなります。

解決するには、コンストラクターで initonly データメンバーを初期化しますが、並列領域の外側で初期化します。

次の例では C3899 が生成されます。

// C3899.cpp
// compile with: /clr /openmp
#include <omp.h>

public ref struct R {
   initonly int x;
   R() {
      x = omp_get_thread_num() + 1000;   // OK
      #pragma omp parallel num_threads(5)
      {
         // cannot assign to 'x' here
         x = omp_get_thread_num() + 1000;   // C3899
         System::Console::WriteLine("thread {0}", omp_get_thread_num());
      }
      x = omp_get_thread_num() + 1000;   // OK
   }
};

int main() {
   R^ r = gcnew R;
   System::Console::WriteLine(r->x);
}