編譯器錯誤 C3899
'var' :不允許直接在類別 'class' 的平行區域內直接使用 initonly 數據成員的 l 值
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);
}