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

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

Patricia Arquette
Patricia ArquetteOriginal
2024-12-15 03:28:08816browse

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

COW Implementation of std::string in C 11: Legality and Restrictions

Copy-on-write (COW) is a technique often used to improve performance by deferring the cost of creating a new object until it is actually modified. However, it has been questioned whether COW is a valid approach for implementing std::string in C 11.

Restriction on COW Implementations

According to the C 11 standard (21.4.1 p6), invalidation of iterators and references is only permitted in specific circumstances:

  • When passed as an argument to a standard library function that takes a reference to non-const basic_string.
  • When calling non-const member functions, excluding operator[], at, front, back, begin, rbegin, end, and rend.

For a COW string implementation, invoking non-const operator[] would necessitate making a copy and invalidating references. This is prohibited by the aforementioned paragraph. As a result, implementing std::string based on COW is no longer deemed legal in C 11.

Explicit Statement of Restriction

The standard does not explicitly state that COW-based std::string implementations are prohibited. However, the restriction is implied by the combination of requirements that explicitly limit invalidation scenarios.

Conclusion

While COW was a viable implementation for std::string in earlier versions of C , the C 11 specifications introduce constraints that prohibit COW-based implementations due to the disallowed invalidation of references.

The above is the detailed content of Is a Copy-on-Write Implementation of `std::string` 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