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:
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!