Its known that Qt widgets use implicit sharing. So I am interested if stl containers std::vector, std::string use implicit sharing too.
If no, why? Since it is very useful.
And if the answer is yes, how we can ascertain in it? I need simple C++ stl program which shows that stl containers use implicit sharing. It doesn’t do deep copy when is copied.
No. They cannot. When you try to modify the contents of the container, or even calling a mutable
begin()on it, it would imply a potential copy-on-write and thus invalidate all references and iterators to the container. This would be a hard to debug situation, and it is prohibited.Although
std::stringis technically not a container, it is still prohibited to do copy-on-write since C++11:[string.require]
Heh, what for? Passing by reference almost always solves all ‘performance problems’. Atomic ref-counts are inherently non-scalable on multi-processors machines.