This code throws a warning when I compile it under VS.
if (m_nDarksideEffectAttCnt < m_DarkTargetIndex.size())
if (m_DuelWatchUserList.size() <= iIndex)
Warning:
& warning C4018: '<=' : signed/unsigned mismatch
Any solutions?
This code throws a warning when I compile it under VS.
if (m_nDarksideEffectAttCnt < m_DarkTargetIndex.size())
if (m_DuelWatchUserList.size() <= iIndex)
Warning:
& warning C4018: '<=' : signed/unsigned mismatch
Any solutions?
As the warning says, this is because of the comparison because of the signed (i.e. m_nDarksideEffectAttCnt, iIndex) and unsigned(i.e m_DuelWatchUserList.size()) types.
In c++20 exactly for this, we have std::cmp_less
#include <utility> // std::cmp_less
if (std::cmp_less(m_nDarksideEffectAttCnt, vec.size()))
{
//
}
This also covers the case, if we mistakenly static_cast the -1 (i.e. int)to unsigned int. That is, the following will not give you an error:
static_assert(1u < -1);
But the following will
static_assert(std::cmp_less(1u, -1)); // error
Also check out the other functions
std::cmp_equal
std::cmp_not_equal
std::cmp_greater
std::cmp_less_equal
std::cmp_greater_equal
m_nDarksideEffectAttCnt and iIndex are of signed types, while (the standard library) size() functions returns an unsigned type (std::size_t).
You can get rid of the warning by changing types of m_nDarksideEffectAttCnt and iIndex to std::size_t or by casting in the comparisons:
if(static_cast<std::size_t>(m_nDarksideEffectAttCnt) < m_DarkTargetIndex.size())
//...
if (m_DuelWatchUserList.size() <= static_cast<std::size_t>(iIndex))
or if iIndex and m_nDarksideEffectAttCnt can have negative values:
if(m_nDarksideEffectAttCnt < static_cast<long long>(m_DarkTargetIndex.size())
//...
if (static_cast<long long>(m_DuelWatchUserList.size()) <= iIndex)