This is quite gross, and only works on gcc
/g++
.
#define STRINGIFY_STR(x) \
std::string(({ std::ostringstream ss; \
ss << "[a: " << x.a \
<< ", f: " << x.f \
<< ", c: " << x.c << "]"; \
ss.str(); })).c_str()
You have to create the string from the values. Please don't do it this way. Please follow Reed's advice.
Here is how I would modify your struct
to allow it to be pretty printed:
struct Str
{
int a;
float f;
char *c;
std::ostream & dump (std::ostream &os) const {
return os << "[a: " << a
<< ", f: " << f
<< ", c: " << c << "]";
}
};
std::ostream & operator << (std::ostream &os, const Str &s) {
return s.dump(os);
}
Str s = {123, 456.789f, "AString"};
Now, to print out s
, you can use std::cout
:
std::cout << s << std::endl;
Or, if you really want a string:
std::stringstream ss;
s.dump(ss);
puts(ss.str().c_str());