Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why gcc-4.9.2 can't support std::string.insert(iterator, range) to return iterator

Tags:

c++

c++11

gcc4.9

According to cppreference, C++11 should support:

template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );

But when I try to compile following code using g++ 4.9.2:

std::string str{ "hello world" }, addition{ "h my" };
auto iter = str.erase(str.begin(), str.begin() + 4);
iter = str.insert(next(iter), addition.begin(), addition.end()); // Error

I receive the following error (live example):

error: no match for 'operator=' (operand types are '__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >' and 'void')
 iter = str.insert(next(iter), addition.begin(), addition.end());
      ^

However, Visual Studio 2013 and Clang seem no problem.

like image 612
pezy Avatar asked Apr 17 '15 04:04

pezy


1 Answers

gcc used a non-conforming copy-on-write (COW) implementation in 4.9.2 they changed this in 5.x series and we can see from a live godbolt session that this is broken in 4.9.2 but works in 5.1.

This change is documented in the GCC 5 Release Series Release notes:

A new implementation of std::string is enabled by default, using the small string optimization instead of copy-on-write reference counting.

We can find a description of the difference between the COW and SSO version from the [libstdc++ mailing list: New std::string implementation (https://gcc.gnu.org/ml/gcc-patches/2014-11/msg01785.html):

This is the long-awaited ABI break for std::string, replacing our venerable Copy-On-Write implementation with a C++11-conforming Small-String-Optimization implementation (based on Paolo's vstring).

The gist of it is adding a second complete std::string implementation that is tagged with abi_tag("cxx11") so it mangles differently (as already done for std::list and std::ios_base::failure).

like image 161
Shafik Yaghmour Avatar answered Oct 27 '22 00:10

Shafik Yaghmour