Is it possible to use std::string in a constexpr?

Using C++11, Ubuntu 14.04, GCC default toolchain. This code fails: constexpr std::string constString = “constString”; error: the type ‘const string {aka const std::basic_string}’ of constexpr variable ‘constString’ is not literal… because… ‘std::basic_string’ has a non-trivial destructor Is it possible to use std::string in aconstexpr? (apparently not…) If so, how? Is there an alternative way to … Read more

How to replace all occurrences of a character in string?

What is the effective way to replace all occurrences of a character with another character in std::string? 17 s 17 std::string doesn’t contain such function but you could use stand-alone replace function from algorithm header. #include <algorithm> #include <string> void some_func() { std::string s = “example string”; std::replace( s.begin(), s.end(), ‘x’, ‘y’); // replace all … Read more

How to trim a std::string?

I’m currently using the following code to right-trim all the std::strings in my programs: std::string s; s.erase(s.find_last_not_of(” \n\r\t”)+1); It works fine, but I wonder if there are some end-cases where it might fail? Of course, answers with elegant alternatives and also left-trim solution are welcome. 48 s 48