Why does std::string have a length() member function in addition to size()?

As per the documentation, these are just synonyms. size() is there to be consistent with other STL containers (like vector, map, etc.) and length() is to be consistent with most peoples’ intuitive notion of character strings. People usually talk about a word, sentence or paragraph’s length, not its size, so length() is there to make … Read more

Why does `std::string::find()` not return the end iterator on failures?

To begin with, the std::string interface is well known to be bloated and inconsistent, see Herb Sutter’s Gotw84 on this topic. But nevertheless, there is a reasoning behind std::string::find returning an index: std::string::substr. This convenience member function operates on indices, e.g. const std::string src = “https://stackoverflow.com/questions/58430288/abcdefghijk”; std::cout << src.substr(2, 5) << “\n”; You could implement … Read more

C++20 constexpr vector and string not working

Your program is actually ill-formed, though the error may be hard to understand. constexpr allocation support in C++20 is limited – you can only have transient allocation. That is, the allocation has to be completely deallocated by the end of constant evaluation. So you cannot write this: int main() { constexpr std::vector<int> v = {1, … Read more

std::string with no free store memory allocation

Andrei Alexandrescu, C++ programmer extraordinaire who wrote “Modern C++ Design” once wrote a great article about building different string implementations with customizable storage systems. His article (linked here) describes how you can do what you’ve described above as a special case of a much more general system that can handle all sorts of clever memory … Read more

Is std::string guaranteed not to give back memory spontaneously?

No guarantee whatsoever. [string.cons]/36 defines assigning a const char* to an std::string in term of a move-assignment, whose definition is: [string.cons]/32 basic_string& operator=(basic_string&& str) noexcept(/*…*/) Effects: Move assigns as a sequence container, except that iterators, pointers and references may be invalidated. This shows that the Committee let the implementation choose freely between an invalidating operation … Read more

Should I use std::string or const char* for string constants? [duplicate]

Usually you should prefer std::string over plain char pointers. Here, however, the char pointer initialized with the string literal has a significant benefit. There are two initializations for static data. The one is called static initialization, and the other is called dynamic initialization. For those objects that are initialized with constant expressions and that are … Read more

Is it possible to use std::string in a constant expression?

As of C++20, yes, but only if the std::string is destroyed by the end of constant evaluation. So while your example will still not compile, something like this will: constexpr std::size_t n = std::string(“hello, world”).size(); However, as of C++17, you can use string_view: constexpr std::string_view sv = “hello, world”; A string_view is a string-like object … Read more

Is it safe to use the std::string::c_str() of a temporary string?

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)