Skip to content
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

sprintf api integer enhancement #255

Closed
rglarix opened this issue Dec 28, 2015 · 3 comments
Closed

sprintf api integer enhancement #255

rglarix opened this issue Dec 28, 2015 · 3 comments

Comments

@rglarix
Copy link
Contributor

rglarix commented Dec 28, 2015

Hi, the sprintf api is unnecessarily converting integer arguments, leading to inexact values.
As an example, the format string "%d" converts its argument to plain int, even if it was long or long long.
While the original sprintf had no type information and must do that, cppformat is smarter and could do much better, for example preserving original type information.
So one could simply write "%d" or "%u" and have cppformat format the string according to the real argument size.
This would be very useful esp. on windows, where some types change size going from 32 to 64bit, but others not, leading to bugs difficult to find.
Obviously you also can use the format api, but then you need to rewrite existing code, while the sprintf api brings immediate benefits just by adding cppformat.

@vitaut
Copy link
Contributor

vitaut commented Dec 30, 2015

Thanks for the suggestion. I don't think it will benefit existing code, because it should already be using correct specifiers and switching to cppformat will have no effect other than introducing type safety. However, this may benefit the new code using cppformat's printf.

@vitaut
Copy link
Contributor

vitaut commented Jan 23, 2016

This is implemented in 8474a62. For example, the code

fmt::printf("%d", std::numeric_limits<long long>::max());

prints 9223372036854775807 now (provided that long long is 64-bit) while previously it printed -1.

Note that only narrowing conversions are disabled, sign and widening conversions still apply.

@vitaut vitaut closed this as completed Jan 23, 2016
@rglarix
Copy link
Contributor Author

rglarix commented Jan 25, 2016

Wonderful! I've been toying with a tentative implementation but yours is much more elegant.
I'll try it ASAP.
Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants