mercredi 2 septembre 2020

C++ vector push_back in GCC 4.4 Vs GCC 4.9 [duplicate]

I have a code like below which compiles fine in GCC 4.9 but throws error in GCC 4.4.

void func() 
{
    map<pair<uint,ulong>, pair<int,int> > myMap();
    vector<myMap::value_type> mapVector();
    ...
    for(myMap::iterator it = myMap.begin(); it != myMap.end(); it++)
    {
        if(...)
            mapVector.push_back(*it);
    }
    ...
}

I get compilation error at mapVector.push_back(*it);

/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:119: error: passing 'const std::pair<unsigned int, long unsigned int>' as 'this' argument of 'std::pair<unsigned int, long unsigned int>& std::pair<unsigned int, long unsigned int>::operator=(const std::pair<unsigned int, long unsigned int>&)' discards qualifiers

I understand dereferencing map iterator produces pair<const Key, T>, so Key assignment (pair here) would fail. But from what I know, vector push_back uses only copy constructor. So why and how this assignment operator is called here?

I saw vector push_back() code in both gcc 4.9.2 and 4.4.7 and they look similar. So how does it compile fine in 4.9.2 but fails with the above error in 4.4.7? Thanks in advance for your help!

Aucun commentaire:

Enregistrer un commentaire