編譯器警告 (層級 4, 關閉) C5240
' attribute-string ': 在這個語法位置中忽略屬性
備註
當 或 [[maybe_unused]]
屬性位於錯誤的語法位置時 [[nodiscard]]
,就會發生警告 C5240。 例如, [[nodiscard]]
這個語法位置中的 屬性會套用至 decl-specifier-seq
,不適用於 函式 f
:
static [[nodiscard]] int f() { return 1; }
在 Visual Studio 2019 16.10 版之前,編譯器會以無訊息方式忽略 [[nodiscard]]
語法位置中未套用至所宣告函式或物件的 或 [[maybe_unused]]
屬性。 在 Visual Studio 2019 16.10 版和更新版本中,編譯器會改為發出預設層級 4 警告 C5240。 如需如何啟用此警告的詳細資訊,請參閱 預設 關閉的編譯器警告。
範例
下列範例示範如何發生警告 5240:
// c5240.cpp
// Compile using: cl /EHsc /W4 /std:c++17 /permissive- /c c5240.cpp
#pragma warning( default: 5240 )
static [[nodiscard]] int f() { return 1; } // C5240
若要修正此問題,請將 屬性移至正確的語法位置:
// c5240_fixed.cpp
// Compile using: cl /EHsc /W4 /std:c++17 /permissive- /c c5240_fixed.cpp
#pragma warning( default: 5240 )
[[nodiscard]] static int f() { return 1; } // OK