分享方式:


編譯器警告 (層級 1) C5056

運算子 ' operator-name ': 已取代陣列類型

備註

在 C++20 中,陣列類型的兩個運算元之間的相等和關係比較已被取代。 如需詳細資訊,請參閱 C++ 標準提案 P1120R0

在 Visual Studio 2019 16.2 版和更新版本中,兩個數組之間的比較作業現在會在啟用編譯器選項時 /std:c++latest 產生層級 1 C5056 警告。 在 Visual Studio 2019 16.11 版和更新版本中,它也會在 下 /std:c++20 產生警告。

範例

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

// C5056.cpp
// Compile using: cl /EHsc /W4 /std:c++latest C5056.cpp
int main() {
    int a[] = { 1, 2, 3 };
    int b[] = { 1, 2, 3 };
    if (a == b) { return 1; } // warning C5056: operator '==': deprecated for array types
}

若要避免警告,您可以比較第一個元素的位址:

// C5056_fixed.cpp
// Compile using: cl /EHsc /W4 /std:c++latest C5056_fixed.cpp
int main() {
    int a[] = { 1, 2, 3 };
    int b[] = { 1, 2, 3 };
    if (&a[0] == &b[0]) { return 1; }
}

若要判斷兩個數組的內容是否相等,請使用 函 std::equal 式:

std::equal(std::begin(a), std::end(a), std::begin(b), std::end(b));