Is std::string ref-counted in GCC 4.x / C++11?

Looking at libstdc++ documentation I find (see the link for more info): A string looks like this: [_Rep] _M_length [basic_string<char>] _M_capacity _M_dataplus _M_refcount _M_p —————-> unnamed array of char_type So, yes it is ref counted. Also, from the discussion here: Yes, std::string will be made non-reference counting at some point, but as a non-reference-counted string … Read more

std::string::c_str() and temporaries

The pointer returned by std::string::c_str() points to memory maintained by the string object. It remains valid until a non-const function is called on the string object, or the string object is destructed. The string object you’re concerned about is a temporary. It will be destructed at the end of the full expression, not before and … Read more

How to concatenate a std::string and an int

In alphabetical order: std::string name = “John”; int age = 21; std::string result; // 1. with Boost result = name + boost::lexical_cast<std::string>(age); // 2. with C++11 result = name + std::to_string(age); // 3. with FastFormat.Format fastformat::fmt(result, “{0}{1}”, name, age); // 4. with FastFormat.Write fastformat::write(result, name, age); // 5. with the {fmt} library result = fmt::format(“{}{}”, … Read more