Legality of COW std::string implementation in C++11

后端 未结 7 1486
天涯浪人
天涯浪人 2020-11-22 09:30

It had been my understanding that copy-on-write is not a viable way to implement a conforming std::string in C++11, but when it came up in discussion recently I

7条回答
  •  甜味超标
    2020-11-22 09:47

    It's not allowed, because as per the standard 21.4.1 p6, invalidation of iterators/references is only allowed for

    — as an argument to any standard library function taking a reference to non-const basic_string as an argument.

    — Calling non-const member functions, except operator[], at, front, back, begin, rbegin, end, and rend.

    For a COW string, calling non-const operator[] would require making a copy (and invalidating references), which is disallowed by the paragraph above. Hence, it's no longer legal to have a COW string in C++11.

提交回复
热议问题