I knew the basics but stumbled a bit on SSO.
Start by explaining that `std::string b = a;` invokes the copy constructor, which typically allocates new memory and copies the character data. Then contrast this with move construction, which transfers ownership without allocation. Finally, discuss Small String Optimization (SSO) and why copy-on-write is not mandated by the standard, emphasizing performance and thread-safety trade-offs.
Pro tip: Mention that SSO avoids heap allocation for short strings, which is why `std::string a = "123";` likely stores the characters inline. Also, note that C++11 banned copy-on-write implementations in practice due to thread-safety and iterator invalidation issues, even though the standard doesn't explicitly forbid it.
Explain that `std::string b = a;` is copy initialization, which calls the copy constructor of `std::string`.
Detail that the copy constructor allocates a new buffer (unless SSO applies) and copies the character data from `a` to `b`.
Explain that if `a` were an rvalue (e.g., `std::string b = std::move(a);`), the move constructor would be invoked, transferring ownership of the buffer without allocation.
Describe how dynamic allocation works for long strings, and how SSO stores short strings inline within the string object to avoid heap allocation.
Explain that the standard does not mandate COW because it complicates thread safety and iterator invalidation; most implementations avoid it for performance and simplicity.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.