Sign in to follow this  

C++ string formatting using a format string

This topic is 2661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fire Lancer
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?)


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.)

Share this post


Link to post
Share on other sites

This topic is 2661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this