Home >Backend Development >C++ >Is a Copy-on-Write std::string Implementation Legal in C 11?

Is a Copy-on-Write std::string Implementation Legal in C 11?

Barbara Streisand
Barbara StreisandOriginal
2024-12-20 00:43:09895browse

Is a Copy-on-Write std::string Implementation Legal in C  11?

Legality of Copy-on-Write (COW) std::string Implementation in C 11

Although it may have been believed that COW is not a feasible technique for implementing a compliant std::string in C 11, recent discussions have raised uncertainties. The following explores the validity of this assumption based on the new standard.

Prohibition of COW-Based std::string Implementations in C 11

C 11 explicitly prohibits COW-based implementations of std::string. This limitation is stated in section 21.4.1 p6 of the standard, which permits invalidation of iterators/references only in specific circumstances:

  • As an argument to a standard library function that accepts a reference to a non-const basic_string.
  • Calling non-const member functions, excluding operator[], at, front, back, begin, rbegin, end, and rend.

In a COW string, invoking non-const operator[] necessitates creating a copy (invalidating references), which is forbidden by the aforementioned paragraph. Consequently, COW strings are prohibited in C 11 due to this constraint.

The above is the detailed content of Is a Copy-on-Write std::string Implementation Legal in C 11?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn