[Solved] PHP’s compilers StringBuilder in C++


This is called string interpolation.

It’s not a mystery, or a secret; there’s an entire section of the PHP manual dedicated to explaining it.

C has printf that does a very similar thing:

const unsigned int MAX_SIZE = 255;
char variable[MAX_SIZE];
snprintf(variable, MAX_SIZE-1, "Var1 has %d value. Var 2 has %d value.", var1, var2);

In C++ you don’t really need string interpolation and may simply build up a string using a std::stringstream:

std::stringstream ss;
ss << "Var1 has " << var1 << " value. Var 2 has " << var2 << " value.";

const std::string variable = ss.str();

For actual templating I like ctemplate.

1

solved PHP’s compilers StringBuilder in C++