Erasing from an STL container by a constant iterator

≯℡__Kan透↙ 提交于 2020-01-03 16:58:49

问题


According the the C++ reference STL containers were fixed in C++11 standard to take constant iterator in the erase methods. The following code would not compile in g++4.7 with c++0x enabled.

#include <vector>

int main()
{
    std::vector<int> vector;
    vector.push_back(0);

    std::vector<int>::const_iterator vectorItr = vector.begin();
    vector.erase(vectorItr);
}

Obviously the new signatures were not implemented. Is there any information when this issue will be fixed? I could not find any respective information in the C++0x/C++11 Support in GCC article.


回答1:


In HERE:

Section: 23.3.6
Description: Class template vector
Status: Partial
Comments: insert and erase members do not take const_iterator arguments (N2350).




回答2:


For what it's worth: I've tested this against GCC 4.8.1 as well as GCC 4.9 (20130602 snapshot) just now, and the result is that 4.8.1 still has this problem (note that the promised C++11 compliance is related to the language core, not the standard library), but the 4.9 snapshot compiles it correctly.

So I guess it's fair to assume that GCC 4.9, once released, will handle this as specified by the Standard.




回答3:


Have a look at gcc's library implementation status. There it clearly states that the feature in question hasn't been implemented yet:

23.3.6 - Class template vector - Partial - insert and erase members do not take const_iterator arguments (N2350).




回答4:


Unsurprisingly: because it has not been implemented yet it seems (even in C++11 mode).

The simplest way for you to check is to have a peek at the underlying implementation of std::vector. Though most of it can be quite arcane, method signatures are usually easy enough to read.




回答5:


Full C++11 compliance is given in GCC 4.8 (not 4.7).

Since 4.7 is not granted to be full compliant, this cannot be considered (In strict technical terms) an "error". May be it was a need to retain some compatibility with other library modules not compliant yet.

If this happens in 4.8 then it has to be considered a bug. But not in 4.7



来源:https://stackoverflow.com/questions/16977823/erasing-from-an-stl-container-by-a-constant-iterator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!