getting cout output to a std::string

You can replace cout by a stringstream. std::stringstream buffer; buffer << “Text” << std::endl; You can access the string using buffer.str(). To use stringstream you need to use the following libraries: #include <string> #include <iostream> #include <sstream>

Can a std::string contain embedded nulls?

Yes you can have embedded nulls in your std::string. Example: std::string s; s.push_back(‘\0’); s.push_back(‘a’); assert(s.length() == 2); Note: std::string‘s c_str() member will always append a null character to the returned char buffer; However, std::string‘s data() member may or may not append a null character to the returned char buffer. Be careful of operator+= One thing … Read more

C++20 with u8, char8_t and std::string

In addition to @lubgr’s answer, the paper char8_t backward compatibility remediation (P1423) discusses several ways how to make std::string with char8_t character arrays. Basically the idea is that you can cast the u8 char array into a “normal” char array to get the same behaviour as C++17 and before, you just have to be a … Read more

Value and size of an uninitialized std::string variable in c++

Because it is not initialized, it is the default constructor that is called. Then : empty string constructor (default constructor) : Constructs an empty string, with a length of zero characters. Take a look : http://www.cplusplus.com/reference/string/string/string/ EDIT : As stated in C++11, §21.4.2/1 : Effects: Constructs an object of class basic_string. The postconditions of this … Read more

Does std::atomic work appropriately?

The standard does not specify a specialization of std::atomic<std::string>, so the generic template <typename T> std::atomic<T> applies. 29.5 [atomics.types.generic] p1 states: There is a generic class template atomic. The type of the template argument T shall be trivially copyable (3.9). There is no statement that the implementation must diagnose violations of this requirement. So either … Read more