   C++ string formatting using a format string
  C++ streams are fine and all, when I know how I want to format something at the time I wrote the code. Often changes to the displayed text are requested by other people I am working with, and so in order to make the changes they have to tell me exactly what they want, so I can change it, recompiled, and send out a new build. As for localisation, the current structure makes it near impossible to support multiple languages, especially should they want variables to be formatted in a different order.The printf family is always an option, however it is far less safe and doesn't support user defined types.Having used Python I have really come to like the format method it provides, since it provides a great deal of flexibility using a string which could be read from an external file.Does such a thing exist with C++ already, is it even possible?I gave it a little thought and figured it could be done with something like the following:A template function for formatting a specific object with overloads for each supported type:template<typename T> void formatObject(const T *object, std::wofstream &output, const std::wstring &format);A printf type method which takes the format string, an array of "objects" (void pointers) and function pointers to the appropriate formatObject specialisations.struct FormatObjectCall{  const void *object;  void (*call)(const void *object, std::wofstream,const std::wstring);};void formatImp(const std::wstring &str, std::wofstream &output, size_t objectCount, FormatObjectCall *objects);A bunch of template methods accepting 0 to say 16 variables to format which build the objects array and call formatImp.
  Did you give boost::format a try?   ;  Looks like a viable solution. I don't really like it anywhere near as much as the Python one and it is missing some things I have used in the past (e.g. their appears to be no right alignment?) and also by the looks of it the performance is pretty terriable compared to other things, but I have never seen this stuff show up as a hotspot so I think that will be OK.Certainly better than spending the next month writing and debugging my proposed solution unless I really need to for some reason.   ;Quote:Original post by Fire LancerLooks like a viable solution. I don't really like it anywhere near as much as the Python one and it is missing some things I have used in the past (e.g. their appears to be no right alignment?)It can do everything printf format specifiers can (and more). When you specify a field width, right-alignment is the default. (Use '-' before the width for left-alignment; '=' - this is added by boost::format and not in printf - for center-alignment.)   