Hello!
I use ICC 2019 update 5 embedded into VS2019 16.1.6. I observed something strange with std::map::insert(). I perform insertion of elements from a std::vector<int> to a std::map<int, int>, using the element value as the map key. Depending on the way I insert them or the compilation switches I use, I don't obtain the same map. See a question on StackOverflow.
Here is the code snippet I used:
#include <map> #include <vector> #include <iostream> std::map<int, int> AddToMapWithDependencyBetweenElementsInLoop(const std::vector<int>& values) { std::map<int, int> myMap; for (int i = 0; i < values.size(); i+=3) { myMap.insert(std::make_pair(values[i], myMap.size())); myMap.insert(std::make_pair(values[i + 1], myMap.size())); myMap.insert(std::make_pair(values[i + 2], myMap.size())); } return myMap; } std::map<int, int> AddToMapOnePerLoop(const std::vector<int>& values) { std::map<int, int> myMap; for (int i = 0; i < values.size(); ++i) { myMap.insert(std::make_pair(values[i], 0)); } return myMap; } int main() { std::vector<int> values{ 6, 7, 15, 5, 4, 12, 13, 16, 11, 10, 9, 14, 0, 1, 2, 3, 8, 17 }; { auto myMap = AddToMapWithDependencyBetweenElementsInLoop(values); for (const auto& keyValuePair : myMap) { std::cout << keyValuePair.first << ", "; } std::cout << std::endl; } { auto myMap = AddToMapOnePerLoop(values); for (const auto& keyValuePair : myMap) { std::cout << keyValuePair.first << ", "; } std::cout << std::endl; } return 0; }
If I compile using "icl mycode.cpp" and I run the program, I obtain this:
0, 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 17,
0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17
(I expected the same number for both lines and 18 numbers on each lines since there are 18 different numbers inserted in the map)
If I compile using "icl /EHsc mycode.cpp" and I run, I obtain:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17,
0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17
(Again, unexpected results?)
If I compile using "icl /Od mycode.cpp" and I run:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
This is the expected result...
Note that I compared using the same switches but with cl.exe (Microsoft compiler) and I always obtain the expected result.
Any idea about the case? Is it a bug? Did I do something wrong? Thanks for your help!