MSVC 2019: Order of comma-separated conditions in C++ for-loop changes warnings

112 views Asked by At

Porting the below code to from VS2017 to 2019 popped up a new warning (this is a simplified version of the original).

main.cpp(18,90): warning C4834: discarding return value of function with 'nodiscard' attribute

#include <map>
#include <vector>

using namespace std;

typedef  map<int, vector<vector<void*> > > MyMap;

const int N = 16;

int main()
{

    MyMap myMap;

    for (auto mapIt = myMap.begin(), mapEnd = myMap.end(); mapIt != mapEnd; mapIt++)
    {
        int j = 0;
        for (auto keyIt = (*mapIt).second.begin(), keyEnd = (*mapIt).second.end(); keyIt != keyEnd, j < N; keyIt++, j++)
        {
            for (auto vecIt = (*keyIt).begin(), vecEnd = (*keyIt).end(); vecIt != vecEnd; vecIt++)
            {
                if (*vecIt)
                {

                }
            }
        }
    }
    
    return 0;
}

Note the for loop has two comma-separated initalizers, two comma-separated conditions, etc. If I remove the j < N condition from the for loop, the warning disappears. If I swap the two conditions, also no warning.

I assume the != operator has some return value that is discarded and that if it is the only condition, or if it is the last condition it is not discarded? Is this expected behavior from the compiler ??

1

There are 1 answers

0
Dave On

Thanks to all! Seems indeed the intent of this legacy code was to && the two conditions. The comma operator was a bug.