Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,399 of 33,346    |
|    =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All    |
|    Re: The std::to_string/to_wstring functi    |
|    24 Aug 11 16:44:20    |
      d327c25a       From: daniel.kruegler@googlemail.com              Am 09.08.2011 03:47, schrieb Ricky65:       >       > C++0x provides the useful std::to_string/to_wstring functions for       > converting numeric values to strings. However, I was disappointed to       > learn that users can't specify the base of the conversion; at the       > moment the std::to_string/to_wstring functions only convert a numeric       > to a base ten string. I think this is an oversight and find this very       > limiting.              I don't think, this is an oversight, it was the result of providing a       consistent specification of tiny little helper functions based on C API.              > In contrast , the similar non-standard but widespread C function itoa       > allows the user to specify the base of the conversion. e.g.       > hexadecimal, octal and binary. For example:       >       > int x = 23;       > char buffer [32];       > itoa(x, buffer, 2) //represent the value as a string in base 2       > (binary)       >       > I find it rather embarrassing that a C function, albeit non-standard,       > has more functionality than these C++ functions.              Sorry, but this is like comparing apples with oranges to me: *Of-course*       some extension will provide more functionality, otherwise it would not       be an extension. I wonder why no-one asks for extending the printf API       to allow formatting of integral types to arbitrary bases between 2 and       36 - this seems to me like the actual defect here.              > I understand that the overloaded std::to_string/to_wstring functions       > call sprintf and wsprintf. The problem with this approach is that       > sprintf can only convert to decimal, hexadecimal and octal bases. Why       > do these functions have to use sprintf? I don't know why it couldn't       > have been left to implementers to write their own versions of these       > functions without these limitations.       >       > It would be preferable if the user could select the base of the       > numeric conversion.              I agree.              > The solution is simple, provide an extra argument for the base with a       > default argument of 10. For example:       >       > std::string to_string(int val, int base = 10);       >       > This would be backward compatible with the current functions as it       > would default to base ten (decimal) conversion as the functions       > currently do whilst at the same time allowing the user to specify the       > base of the numeric conversion.              This is not an interface question. Of-course it could be declared this       way. But there is currently no way to specify the semantics of such a       function similarly to the other ones. My suggestion would be to ask the       C committee to extend (s)printf for extended formatting capabilities for       integral types to fix this problem.              And if this would be fixed, one could also consider to generalize the       basefield manipulators ([basefield.manip]) for arbitrary base values.              > If something like this doesn't get implemented, I suspect people will       > use itoa or its variants, or roll their own similar function, reducing       > the utility of the std::to_string/to_wstring functions.              Unless C does not provide extended formatting choices, they will use       itoa, no matter what. I strongly suggest to ask C for extending the       formatting choices in regard to fix this.              HTH & Greetings from Bremen,              - Daniel Krügler                     --        [ See http://www.gotw.ca/resources/clcm.htm for info about ]        [ comp.lang.c++.moderated. First time posters: Do this! ]              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca