Potentially swap argument orderCXX-W2058
Found potentially swapped arguments in a C++ function call. This issue is raised when there are implicit conversions between the arguments of a function call, indicating that the arguments may have been swapped by mistake.
This issue arises when two arguments of different types are passed to a function, and there exists an implicit conversion between the two types. This can lead to unexpected behavior and incorrect results.
For example, consider the following function:
void func(int x, char y) {
// Swap the values of x and y
// ...
}
If this function is called with arguments of different types, such as func('a', 10)
, the compiler will perform an implicit conversion to match the types of the arguments. In this case, the arguments will be swapped, resulting in a call to func(10, 'a')
. This can lead to unexpected behavior and incorrect results.
To fix this issue, ensure that the arguments are passed in the correct order to the function.
Bad practice
void F(int, double);
template <typename T, typename U> void G(T a, U b) {
F(a, b);
F(2.0, 4); // possibly swapped
}
Recommended
void F(int, double);
template <typename T, typename U> void G(T a, U b) {
F(a, b);
F(4, 2.0); // fine
}