Binary Conversion IO

Ed Smith-Rowland 3dw4rd@verizon.net
Mon Sep 16 14:20:00 GMT 2013


On 09/16/2013 08:26 AM, Daniel Krügler wrote:
> 2013/9/13 Ed Smith-Rowland <3dw4rd@verizon.net 
> <mailto:3dw4rd@verizon.net>>
>
>     Greetings,
>
>     I am putting together a tiny proposal for the C++ standard library
>     inspired by the C++14 acceptance of binary literals in the core
>     language (which we have, and really mostly had since gcc-4.3ish).
>     Before I stuck my neck out I wanted to make sure the feature was
>     implementable in libstdc++.
>
>     The attached patch adds an enumerator, bin, to ios_base to go
>     along with oct, dec, and hex.  Similarly, there is a bin
>     manipulator. Additions to num_put and num_get write and read
>     binary numbers.  As implemented, I have it as an extension to
>     C++98 (not isolated by std flags) since we've had binary literals
>     available in that version too.
>
>     This patch bootstraps and tests clean on x86_64-linux.
>
>     1. Is this a useful/amusing idea (I like it for reading and
>     writing flags) suitable for the library.  I like the consistency
>     with literals and the rest of the library.
>
>     2. Can anyone see any showstoppers in this or any other
>     implementation?
>
>
> While I'm certainly in favour to see such a bin manipulator 
> standardized, I wonder whether for a valid extension this doesn't need 
> to have an "implementation-reserved" name, for example "__bin", 
> otherwise wouldn't it conflict with a user-defined "bin" macro name? A 
> quick search in the C++/C11 standard doesn't gave me any entity named 
> this way yet, so this seems currently a valid one. In regard to the 
> technical way to define a flag value representing that format I would 
> like to point out that there might be resistance for new flag values 
> given the fact that the support for the C99 hexfloat format was IMO 
> intentionally provided by an otherwise invalid flag combination 
> instead of a new one.
>
> - Daniel
>
>
I see your point about the flags.  Maybe, instead of adding a new flag 
we could do something like this:

str.setf(std::ios_base::oct 
<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags> | 
std::ios_base::hex 
<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>, 
std::ios_base::fmtfield 
<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>) in analogy with 
hexfloat.

As for the manipulator name I think it suffers from the same potential 
for name collision as any other std library component.  The user would 
just have to qualify with std::bin.  OTOH, I guess I could see bin used 
as a variable in user code which would break with 'using namespace std;'.

Maybe std::binint ;-)

Ed



More information about the Libstdc++ mailing list