編譯器錯誤 C2482
'identifier' :Managed/WinRT 程式代碼中不允許的 'thread' 數據的動態初始化
備註
在 Managed 或 WinRT 程式代碼中,使用 __declspec(thread) 儲存類別修飾詞屬性或 thread_local 儲存類別規範宣告的變數,無法使用需要在運行時間評估的表達式初始化。 需要靜態表達式,才能初始化 __declspec(thread)
這些 thread_local
運行時間環境中的數據。
範例
下列範例會在Managed (/clr) 和 WinRT (/ZW) 程式代碼中產生 C2482:
// C2482.cpp
// For managed example, compile with: cl /EHsc /c /clr C2482.cpp
// For WinRT example, compile with: cl /EHsc /c /ZW C2482.cpp
#define Thread __declspec( thread )
Thread int tls_i1 = tls_i1; // C2482
int j = j; // OK in C++; C error
Thread int tls_i2 = sizeof( tls_i2 ); // Okay in C and C++
若要修正此問題,請使用常數、 constexpr
或靜態表達式來初始化線程本機記憶體。 個別執行任何線程特定的初始化。