I was just reading a Wikipedia article on Copy-on-write (curious if there are any filesystems that support it), and was surprised by the following passage:
COW is also used outside the kernel, in library, application and system code. The string class provided by the C++ standard library, for example, was specifically designed to allow copy-on-write implementations:
std::string x("Hello");
std::string y = x; // x and y use the same buffer
y += ", World!"; // now y uses a different buffer
// x still uses the same old buffer
I didn't know that copy-on-write was every supported in STL. Is that true? Does it apply to other STL classes, e.g. std::vector
or std::array
? Which compilers support that optimization (in particular, I wonder about G++, Intel C++ compiler and Microsoft C++ compiler)?
std::string
cannot be copy-on-write anymore, see here. – Incunabulumstd::vector
andstd::array
rule out COW for those types. And requirements imposed onstd::string
in C++11 rule out COW for strings too. – Rigveda