Is such difference between mingw and msvc normal

Today we found a bug at work, which can be simplified to this:

std::map<int, Obj*> workMap;

for(auto a : workMap)
{
    workMap.erase(a.first);
} 

Actually, we invoked function, that invoked function that did erase, but it is not the point.

The point is, that on my computer with visual studio compiler I've got list iterator is not incrementable error , while my colleague with mingw has got no errors at all!

I understand, that we have different compilers and different implementations of STL(probably). It is normal, that they could have different warnings or flags to suppress warnings. But it is different runtime error behaviour. Is this normal? And can I somehow "enable" all runtime errors?


These types of for-loops are not for changing the container itself. You invalidate iterators you still use, thus anything can happen. Since this is undefined behavior, the compiler is allowed to generate code that does anything, there are no guarantees what so ever, so both compilers are correct.

Your observation is caused by the fact that msvc enables some runtime checking for debug builds by default, gcc does not.

You can enable runtime checking for libstd++ , the standard library implementation of gcc, with -D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC during compilation. It will show an error in your example. (Live)


Just so we're all clear, this code:

std::map<int, Obj*> workMap
for(auto a : workMap)
    workMap.erase(a.first);

causes undefined behavior and should not be expected to work properly.

Visual Studio is simply being helpful and providing a runtime error about this. It's not obligated to do this.

链接地址: http://www.djcxy.com/p/41900.html

上一篇: GCC二进制文件和MSVC二进制文件之间的区别?

下一篇: mingw和msvc之间的这种区别是正常的