|
|
Subscribe / Log in / New account

Overloading

Overloading

Posted Apr 27, 2014 19:03 UTC (Sun) by rleigh (guest, #14622)
In reply to: Overloading by khim
Parent article: Porting the Go compiler to Go

i18n? Use gettext to localise Boost.Format strings and stream the resulting formatted string. This has all the ease of translation and control with format specifiers you'd get with C printf strings, with the added advantage of being completely type-safe and safe at runtime if for whatever reason you pass the wrong type or number of args. Even better, it's not restricted to the small number of basic types you can use with C printf; any C++ class with an overloaded ostream operator may be used. This makes for a much more robust solution than you get with C printf. And if the translator screws up the format string, it won't crash your program.

Trivial example:

std::string format_string = _("[%1% chroot] (%2%→%3%) Running login shell: ‘%4%’");
boost::format fmt(format_string);
fmt % session_name() % olduser() % newuser() % shell;
std::cout << fmt << std::endl;

This could of course be combined as:

std::cout << boost::format(_("[%1% chroot] (%2%→%3%) Running login shell: ‘%4%’"))
% session_name() % olduser() % newuser() % shell << std::endl;

Adapted from here

Regards, Roger


to post comments

Overloading

Posted Apr 27, 2014 19:06 UTC (Sun) by mpr22 (subscriber, #60784) [Link]

And yet, I still find iostream syntax to be syntactic ipecac.

Overloading

Posted Apr 27, 2014 21:25 UTC (Sun) by khim (subscriber, #9252) [Link]

Boost.Format is actually well-designed library (I don't like it's abuse of operator%, but that's matter of taste), yet you can easily use it with stdio functions, you don't need streams. Unfortunately it does not come with standard library while printf does.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds