Muokkaa

Jaa


Compiler warning (level 1) C5056

operator 'operator-name': deprecated for array types

Remarks

Equality and relational comparisons between two operands of array type are deprecated in C++20. For more information, see C++ Standard proposal P1120R0.

In Visual Studio 2019 version 16.2 and later, a comparison operation between two arrays (despite rank and extent similarities) now produces a level 1 C5056 warning when the /std:c++latest compiler option is enabled. In Visual Studio 2019 version 16.11 and later, it also produces a warning under /std:c++20.

Example

In Visual Studio 2019 version 16.2 and later, the following code produces warning C5056 when the /std:c++latest compiler option is enabled. In Visual Studio 2019 version 16.11 and later, it also produces a warning under /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
}

To avoid the warning, you can compare the addresses of the first elements:

// 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; }
}

To determine whether the contents of two arrays are equal, use the std::equal function:

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