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

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

Susan Sarandon
Susan SarandonOriginal
2024-12-25 14:32:11251browse

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

Legality of COW std::string Implementation in C 11

In earlier versions of C , copy-on-write (COW) was a viable implementation strategy for std::string. However, concerns have been raised about its validity in C 11. This article examines the legality of COW-based std::string implementations in C 11.

Question: Does C 11 explicitly prohibit COW-based std::string implementations?

Answer: No, C 11 does not explicitly state that COW-based implementations are prohibited.

Question: If not, how is this restriction implied?

Answer: The restriction arises from the new requirements in C 11 regarding iterator and reference invalidation. According to section 21.4.1 p6 of the standard, invalidation is only permitted in specific cases:

  • Passing as an argument to specific standard library functions that accept non-const basic_string references.
  • Calling non-const member functions, excluding operator[], at, front, back, begin, rbegin, end, and rend.

In a COW string implementation, calling non-const operator[] would necessitate a copy and reference invalidation. However, the aforementioned paragraph prohibits this. Therefore, COW std::string implementations are no longer permissible in C 11.

The above is the detailed content of Is 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