Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Sane string conversion in C++11

Credits go to Audric, bamccaig, Chris Katko, Kitty Cat, l j, Neil Roy, Polybios, and Samuel Henderson for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Sane string conversion in C++11
Chris Katko
Member #1,881
January 2002
avatar

D:

literally_anything.toString();

Because all objects inherit from the object class and have a toString method.

Also,

to!anything_else(almost_anything);

:)

It makes (un)serializing really easy to a file for loading/saving maps.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Audric
Member #907
January 2001

I haven't seen it mentioned so far, but GNU's and BSD's libc include asprintf() : It's a sprintf() variant where you don't pass your own target buffer. It computes how long the string will be, performs a malloc(), fills it, and returns the address to you. It's then your responsibility to free() it when you're done with it.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

bamccaig
Member #7,536
July 2006
avatar

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

bamccaig said:

Looks like you can't pass a std::string to StringPrintF though.

Not currently. It might be possible with a bit of a hack, but you would have to find a non-reserved format specifier to use and be consistent with it.

There's a bit of a problem with StringPrintF though, and that is when you create a temporary string with it like so :

printf("%s\n" , StringPrintF("Hello my name is dangling pointer").c_str());

 1   2 


Go to: