Programmatically determine if std::string uses Copy-On-Write (COW) mechanism

荒凉一梦 提交于 2019-12-03 08:32:23

Using &s1[0] to take the adress is not what you want, [0] returns a writable reference and will create a copy.

Use data() instead, it returns a const char*, and your tests may pass.

The copy-on-write paradigm is dependent on knowing when you are doing a write. This will occur whenever the object is returning a writable reference.

If you work with const references to the strings, you may be able to compare the addresses if the class was specialized to disable the copy when returning a const reference to the data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!