Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The short story: in C++ code using `std::string` never use string literals except to initialize static-scoped `std::string` constants.
(And by the way: CppTips/NeverUseStaticLocalVariables)

The long story: `std::string` is all about avoiding copies. Reference counting and copy-on-write serve to maximise the sharing of a single heap-allocated char array while maintaining memory safety. When used consistently in a program it works rather nicely.

...

Code Block
namespace { 
   const std::string end("end");
}
void f() { std::string x; while (x != end) {...} }

And once again NeverUseLocalStaticVariables. NeverUseStaticLocalVariables