C++ warning C4018: '' : signed/unsigned mismatch
Replace all the definitions of int i
with size_t i
.
std::vector<T>::size()
returns the type size_t
which is unsigned (since it doesn't make sense for containers to contain a negative number of elements).
warning C4018: '' : signed/unsigned mismatch
length() probably returns size_t or unsigned long and you are comparing it with signed long.
Change
long lDelFront = 0, lDelBack = 0;
to
size_t lDelFront = 0;
size_t lDelBack = 0;
to avoid signed/unsigned comparison
“signed/unsigned mismatch” warnings (C4018) with decrementing for loop
Firstly, learn a few well-established idioms that'd allow you to write decrementing cycles using unsigned
types. This is an absolutely essential skill in C and C++. E.g.
for ([some unsigned type] i = N; i-- > 0; )
{
// Process i-th element
}
or
for ([some unsigned type] i = N; i > 0; )
{
--i;
// Process i-th element
}
(Note, that the initial value of i
is N
, not N - 1
.)
Or you can even use a "more natural" approach
for ([some unsigned type] i = N - 1; i != -1; --i)
{
// Process i-th element
}
Secondly, start using the proper unsigned type for vector indexing. In your case
for (auto i = vector.size() - 1; i != -1; --i)
or
for ([your vector type]::size_type i = vector.size() - 1; i != -1; --i)
That will solve your warning issue.
Alternatively, you can choose a "loser's way out" and just suppress the warning by using an explicit cast
if ((unsigned) i < vector.size() / 2)
or just do #pragma warning(disable: 4018)
.
How to deal with “signed/unsigned mismatch” warnings (C4018, no loop)?
To get answer which value is bigger use signed arbitrary arithmetic types like boost::cpp_int
auto v1 = int(-3);
auto v2 = size_t(31);
boost::cpp_int(v1) >= boost::cpp_int(v2);
If you have access to 80bit MMX registers then consider using them to store both values as signed and compare.
replacing macro with a function causes signed/unsigned mismatch warning
Move the problem out of your code by calling std::cmp_not_equal
const auto f = [](auto&& lhs, auto&& rhs) {
if (std::cmp_not_equal(lhs, rhs)) { blah blah }
};
This family of functions, added in C++20, is defined in a way that properly compares integer arguments even in the presence of a signed/unsigned mismatch.
Now the Standard library author is responsible for writing "all that template gunk" and all you need to do extra is #include <utility>
For the specific case of assertions, you need a macro anyway to capture information.
#define ASSERT_EQ(actual, expected) do { \
if (lhs == rhs) break; \
log_assertion_failure(__FILE__, __LINE__, #actual, actual, expected); \
} while(0)
Related Topics
C++ Sizeof(Array) Return Twice the Array's Declared Length
How to Enforce Copy Elision, Why It Won't Work with Deleted Copy Constructor
Is Left-Shifting (<<) a Negative Integer Undefined Behavior in C++11
What Are Your Favorite C++ Coding Style Idioms
How to Avoid Static Member Function When Using Gsl with C++
Undefined Reference to 'Pthread_Key_Create' (Linker Error)
Opengl Gl_Polygon Concave Polygon Doesn't Color In
Must New Always Be Followed by Delete
Creating, Opening and Printing a Word File from C++
Global Function Definition in Header File - How to Avoid Duplicated Symbol Linkage Error
How to Find a Search Term in Source Code
Write Concurrently Vector<Bool>
Rdrand and Rdseed Intrinsics on Various Compilers
For Purposes of Ordering, Is Atomic Read-Modify-Write One Operation or Two