Here's the one-liner solution:
#include <iostream>
#include <string>
int main() {
std::string s = std::string("Hi") + " there" + " friends";
std::cout << s << std::endl;
std::string r = std::string("Magic number: ") + std::to_string(13) + "!";
std::cout << r << std::endl;
return 0;
}
Although it's a tiny bit ugly, I think it's about as clean as you cat get in C++.
We are casting the first argument to a std::string
and then using the (left to right) evaluation order of operator+
to ensure that its left operand is always a std::string
. In this manner, we concatenate the std::string
on the left with the const char *
operand on the right and return another std::string
, cascading the effect.
Note: there are a few options for the right operand, including const char *
, std::string
, and char
.
It's up to you to decide whether the magic number is 13 or 6227020800.