Home >Backend Development >C++ >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:
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!