skip to content

Why did C++11 rule out copy-on-write implementations of std::string, and what did that change cost libstdc++ users?

level: seniorimportance: should knowfreq 35%

answer

  1. C++03 allowed reference counting
  2. operator[] may not invalidate
  3. unsharing allocates and can throw
  4. data races on hidden sharing
  5. GCC 5 dual ABI

basics

~20 s

C++11's invalidation rules forbid it: non-const operator[], at, data, begin and end may not invalidate pointers or iterators, but a copy-on-write string must unshare, allocating a new buffer, on that first write. libstdc++ needed a new std::string ABI in GCC 5.

solid answer

~50 s

Under C++03 a `std::string` could share one reference-counted buffer among copies and copy only on the first write. C++11 tightened the rules so that this no longer conforms: references, pointers and iterators into a string may be invalidated only by non-const member functions **other than** `operator[]`, `at`, `data`, `front`, `back`, `begin`, `end`, `rbegin` and `rend`, and `operator[]` throws nothing. A COW string must unshare on exactly those calls, which allocates (and so can throw) and moves the string's characters, invalidating pointers taken earlier. The standard's own compatibility note says it plainly: reference-counted strings are no longer allowed because invalidation is subtly different with them. Hidden sharing also forces atomic, or otherwise synchronized, reference counts so that copies used on different threads stay race-free. libstdc++ shipped COW strings until GCC 5.1 introduced `std::__cxx11::basic_string` behind `_GLIBCXX_USE_CXX11_ABI`, splitting the ABI.

code

cpp · 15 lines
cpp
#include <iostream>
#include <string>

int main() {
    std::string a = "https://example.com/api/v1/players";   // long enough to live on the heap
    const char* p = a.data();     // pointer into a's characters

    std::string b = a;            // a copy: a is passed as const, nothing of a is invalidated
    a[8] = 'E';                   // non-const operator[]: also may not invalidate p

    std::cout << std::boolalpha
              << "p still points into a: " << (p == a.data()) << '\n'
              << "a: " << a << '\n'
              << "b: " << b << '\n';
}

go deeper

for a junior

Know that a modern std::string copy duplicates the characters and that sharing copies were allowed only before C++11.

for a middle

Explain how copy-on-write works and which C++11 invalidation rule it breaks when operator[] must unshare.

for a senior

Connect the rule to practice: the libstdc++ dual ABI, std::__cxx11 link errors, and rebuilding prebuilt dependencies with one ABI setting.

for a principal

Use the change as a case study in ABI cost: a library type's layout is a contract, and fixing it can take years and a toolchain migration.

## What copy-on-write was A **copy-on-write (COW)** string makes copies cheap by sharing: `std::string b = a;` increments a **reference count** on a's buffer instead of copying characters. Only when one of the sharers is about to be modified does it **unshare**: allocate its own buffer, copy the characters, decrement the shared count, and write. C++98 and C++03 allowed this, and libstdc++ (GCC's library) implemented `std::string` this way for years. ## The C++11 rules that COW cannot meet C++11 rewrote the requirements for `basic_string`. Two pieces matter. **1. Which operations may invalidate.** Pointers, references and iterators into a string may be invalidated only by: - passing the string as a non-const reference to a standard library function (`swap`, `operator>>`, `std::getline`); - calling a non-const member function **other than** `operator[]`, `at`, `data`, `front`, `back`, `begin`, `rbegin`, `end` and `rend`. Those exempt members are exactly where a COW string has to unshare, because they hand out mutable access to the characters. Unsharing moves the string to a new buffer, so a pointer taken before the call would now point at the other sharer's text. C++11 says that may not happen. **2. `operator[]` throws nothing.** Its specification lists *Throws: Nothing.* A COW unshare allocates, and allocation can throw `std::bad_alloc`. The standard's compatibility annex records the outcome against C++03: `basic_string` requirements "no longer allow reference-counted strings", with the rationale "invalidation is subtly different with reference-counted strings". A second entry loosens other invalidation rules to "allow small-string optimization" and notes that `data` and `c_str` no longer invalidate iterators. ## The aliasing bug the rules prevent ```cpp std::string a = "https://example.com/api/v1/players"; const char* p = a.data(); // pointer into a std::string b = a; // COW: a and b share one buffer a[8] = 'E'; // COW: a unshares here, p is left pointing at b's buffer ``` Under C++11, the copy takes `a` by const reference and so cannot invalidate `p`, and `a[8]` is exempt too, so `p` must still point into `a`. The runnable version below prints `true` with libc++. With a COW library the same code would leave `p` aimed at the buffer that `b` now owns alone; the mechanism above is the reason, though that behaviour cannot be run on this machine's library. ## Threads made COW costly C++11 also introduced a memory model and a rule for the library: a standard function may modify objects visible to other threads only through its non-const arguments. Two threads each working on their own `std::string` must not race, even if, under COW, those strings secretly share a buffer. A COW implementation can stay correct only with **atomic** (or otherwise synchronized) reference-count updates on every copy and destruction and careful unsharing, which costs more than the copies it saves for short strings. The invalidation rules decide the question; the threading cost explains why few mourned the change. ## What it cost libstdc++ users Changing `std::string`'s layout breaks binary compatibility, so GCC took years to follow. In **GCC 5.1** libstdc++ introduced a new ABI with a conforming `std::string` (and a `std::list` that tracks its size), placed in an inline namespace so the new type is `std::__cxx11::basic_string` while the old one keeps its old mangled name. - The macro `_GLIBCXX_USE_CXX11_ABI` picks the ABI per translation unit; with GCC's default configuration it is `1` (new ABI), though some distributions configured GCC 5 to default to `0`. - Mixing objects or libraries built with different settings typically fails at link time with unresolved symbols that mention `std::__cxx11`, because the two string types have different mangled names. - Projects that link prebuilt binaries had to rebuild them or pin the old ABI with `-D_GLIBCXX_USE_CXX11_ABI=0`. ## What took COW's place Copy-on-write existed to make copies cheap. C++11 recovered most of that benefit by other means, each without hidden sharing: 1. **Move semantics**: returning or passing an rvalue string transfers its heap buffer in constant time. 2. **Small-string optimization**: short strings, the majority in practice, copy without any allocation. 3. **Non-owning parameters**: functions that only read take `const std::string&` or, since C++17, a `std::string_view`, so no copy happens at all. ## Takeaways for an interview - C++11 forbade COW through the invalidation rules and the non-throwing `operator[]`. - Copies became deep copies; SSO and move semantics recovered most of the speed. - The libstdc++ dual ABI is the visible scar, and `std::__cxx11` in a linker error points straight at it.

  • What does an undefined reference to std::__cxx11::basic_string usually mean on Linux?
    Two parts of the program were compiled against different libstdc++ string ABIs. One side was built with `_GLIBCXX_USE_CXX11_ABI=1`, the GCC default since 5.1, and expects `std::__cxx11::basic_string`; the other, often a prebuilt library, was built with the old copy-on-write ABI. Rebuild the dependency with the same setting, or compile everything with the matching macro value.
  • If copies are deep since C++11, what makes passing strings around cheap again?
    Three things. Move semantics transfer a heap buffer in constant time when the source is an rvalue. The small-string optimization makes short copies allocation-free. And APIs that only read take `const std::string&` or `std::string_view`, so no copy happens at all. Together they cover the cases COW used to optimize, without hidden sharing.

  • C++11 banned copy-on-write only because reference counting is slow.
  • GCC's std::string is still copy-on-write by default today.
  • A copy-on-write string can unshare without ever allocating.
  • Non-const operator[] on a std::string may invalidate earlier pointers.
  • Copy-on-write strings are thread-safe without atomic operations.