When initializing a std::string like this:
std::string { "hello" + " c++ " + " world" }; // error It errors because const char* cannot be added.
Meanwhile:
std::string { "hello" " c++ " " world" }; // ok It works thanks to compiler magic.
And then, according to Google's coding-guide, I use constexpr since statements should be not be hard coded:
constexpr const char* HELLO = "hello"; constexpr const char* LANG_CPP = " c++"; constexpr const char* WORLD = " world"; Now things are different:
std::string str { HELLO LANG_CPP WORLD }; // error Implicit concatenation doesn't work anymore.
Eventually, I wrote code like this:
std::string str = HELLO; str += LANG_CPP; str += WORLD; Other options like below are not appealing to me:
std::string str = std::string(HELLO) + std::string(LANG_CPP) + std::string(WORLD); std::string str = HELLO + std::string(LANG_CPP) + WORLD; Do you have alternatives that are better looking?
Update: I have simplified the code. In order to focus on the matter I am concerned with.
My original code looked like this:
sql = "delete from " TABLE_NAME ";"; sql = "insert into " TABLE_NAME "values(" var1 ", " var2 ");"; By the way, the result of concatenation is not required to be a string, const char* is ok, too.