Create inline std :: string

Is it possible to initialize std::stringwithout creating a variable?

What I want to do:

throw std::runtime_error("Error: " + strerror(errno));

What am I doing now:

std::string error = "Error: ";
std::string errmsg(strerror(errno));
throw std::runtime_error(error + errmsg);
+4
source share
1 answer

Just pause one of them:

throw std::runtime_error(std::string("Error: ") + strerror(errno));

Overload for can take , as well . operator+std::stringconst char*std::string

If you have access to C ++ 14, you can using namespace std::literalsdo it as follows:

throw std::runtime_error("Error: "s + strerror(errno));
+6
source

Source: https://habr.com/ru/post/1682085/


All Articles