You can wrap std::stringstream
in a function and use that:
#include <string>
#include <sstream>
void foo(std::string){}
template<typename...Args>
std::string concat(Args&&...args){
std::stringstream ss;
(ss << ... << args);
return ss.str();
}
int main(){
int intValue=12;
std::string stringValue="hello";
foo(concat("test ",intValue," ",stringValue,"..."));
}
std::string
supports concatenation by +
, but primitives types do not. So, you can wrap them in std::to_string
but that is not so nice.
Allowing "hello"+5
is too dangerous because "hello"
is one easy step from being const char*
which would trigger pointer arithmetic instead.
From C++20, there is std::format
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…