共用方式為


編譯器警告 (層級 4) C5054

運算子 'operator-name': 在不同類型的列舉之間已被取代

備註

C++20 已取代操作數上的一般算術轉換,其中一個操作數是列舉型別,另一個則是不同的列舉型別。 如需詳細資訊,請參閱C++標準提案 P1120R0

在 Visual Studio 2019 16.2 版和更新版本中,列舉型別之間的隱含轉換會在啟用編譯程式選項時 /std:c++latest 產生層級 4 警告。 在 Visual Studio 2019 16.11 版和更新版本中,它也會在下 /std:c++20產生警告。

範例

在 Visual Studio 2019 16.2 版和更新版本中,下列程式代碼會在啟用編譯程式選項時 /std:c++latest 產生層級 4 警告。 在 Visual Studio 2019 16.11 版和更新版本中,它也會在 底下 /std:c++20產生警告:

// C5054.cpp
// Compile using: cl /EHsc /W4 /std:c++latest C5054.cpp
enum E1 { a };
enum E2 { b };
int main() {
    int i = a | b; // warning C5054: operator '|': deprecated between enumerations of different types
}

若要避免警告,請使用 static_cast (部分機器翻譯) 來轉換第二個運算元:

// C5054_fixed.cpp
// Compile using: cl /EHsc /W4 /std:c++latest C5054_fixed.cpp
enum E1 { a };
enum E2 { b };
int main() {
  int i = a | static_cast<int>(b);
}