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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With