-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for formatting via variadic templates where possible #25
Comments
This is a very good idea. The implementation can be substantially simplified though: see https://github.com/vitaut/format/blob/master/format-test.cc#L1490 - it only needs to be parameterized on character type. What do you think? |
Ah, I didn't realise a formatter could already take an initialisation list (I'm not sure that was available in the version I was using). Yes, I agree, that is much simpler :) So this would basically only need to add the code to work out if variadic templates were supported, and if so, add some extra Format functions like so?
Would you like me to roll that into a pull-request? EDIT: |
Exactly, it could be even simplified a little bit further by parameterizing on character type: template<typename Char, typename... Args> std::basic_string<Char> Format(const BasicStringRef<Char> ...); or something like that. Sure, feel free to submit a pull request and thanks for working on this. |
I had thought about templating it further like that, but it saves little in the way of code duplication and adds extra syntax to the calling code to specify the char type:
Based on that, I feel having the two separate functions is cleaner. I'll try and take a look at this shortly. |
Making the calling code more complicated is not worth it of course, but isn't the Char type deduced from the first argument? |
Ah, it doesn't. Two overloads (one for char and another for wchar_t) is fine then. |
I feel that C++11 variadic templates would provide a nice alternative to the streaming syntax that format uses by default.
I've implemented a version of this for my own project, and have had a go at rolling it back into format itself in a more generic form.
The change can be found here: https://github.com/jdale88/format/commit/5a2eb12a783246f87c1a0d1b5355fe8df22ec6d2
At present, the syntax for using that change is as follows (it will accept any number of parameters after the initial format string):
I felt it best to discuss this feature before submitting a pull request, as it might be completely unsuitable, or you might want to approach it differently. This bug is really just to open up a discussion about it.
Thanks.
EDIT:
I improved upon that initial commit a bit.
Moved the formatter type onto the functions rather than the struct. This makes it much easier to make the struct a friend of BasicFormatter (so that it can access Arg) without having to adjust the visibility of Arg or add extra friends.
Added overloads for Format to call VariadicFormat with the default formatter; used as shown below:
The text was updated successfully, but these errors were encountered: