6

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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
pezy
  • 1,022
  • 2
  • 8
  • 27
  • 5
    GCC 4.9's `string` is not C++11-conforming in numerous ways; this is just one of them. – T.C. Apr 17 '15 at 04:34
  • @T.C. where can I find out more details about this? – pezy Apr 17 '15 at 04:40
  • 1
    Looking at the function definition in 4.9.2, the first parameter type is `iterator` (pre-C++11) instead of `const_iterator`. Looks like it has been [fixed](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/basic_string.h#L1269) on libstdc++ trunk. Presumably destined for gcc5. – Praetorian Apr 17 '15 at 04:53
  • Didn't read the actual error you were observing. The culprit is not `iterator` vs `const_iterator` but the return type. The gcc4.9.2 version of this `insert` overload returns `void`. – Praetorian Apr 17 '15 at 05:14

1 Answers1

3

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).

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740