C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::bin manipulator for integer binary input/output

From: Pavel Vazharov <freakpv_at_[hidden]>
Date: Tue, 11 Feb 2025 08:28:25 +0200
On Mon, Feb 10, 2025 at 9:34 PM Simon Schröder via Std-Proposals <
std-proposals_at_[hidden]> wrote:
>
> Well, I agree that we don‘t necessarily need any extensions of ostreams
because we have std::format and std::print (though nothing equivalent to
std::print for writing to files directly). But, there not a replacement for
istreams, yet. Is there any proposal to replace istreams?
The std::print(ln) functions can write to files
<https://en.cppreference.com/w/cpp/io/print>. Or maybe I misunderstood you?
>
> Simon
>
> On Feb 10, 2025, at 7:31 PM, Jeremy Rifkin via Std-Proposals <
std-proposals_at_[hidden]> wrote:
>
> 
> Hi,
> I would be surprised if this hasn’t been proposed before. Now that we
have std::format, I think it’s hard to make an argument for adding anything
formatting-related to iostreams.
>
> Cheers,
> Jeremy
>
> On Mon, Feb 10, 2025 at 12:01 Javier Estrada via Std-Proposals <
std-proposals_at_[hidden]> wrote:
>>
>> There is a manipulator gap to read/write integer numbers in binary. As
you well know, the standard manipulators are std::oct, std::dec, std::hex,
which is essentially a format flag onto ios_base::basefield.
>>
>> Having a dedicated manipulator would close that gap and would eliminate
the current "trick" of using std::bitset. The (abbreviated) example in
cppreference .com reads:
>>
>> #include <bitset>
>> #include <iostream>
>>
>> int main()
>> {
>> std::cout << "The number 42 in octal: " << std::oct << 42 << '\n'
>> << "The number 42 in decimal: " << std::dec << 42 << '\n'
>> << "The number 42 in hex: " << std::hex << 42 << '\n';
>>
>> // Note: there is no I/O manipulator that sets up a stream to print
out
>> // numbers in binary format (e.g. bin). If binary output is necessary
>> // the std::bitset trick can be used:
>> std::cout << "The number 42 in binary: " << std::bitset<8>{42} <<
'\n';
>>
>> }
>>
>> With a std::bin manipulator:
>>
>> #include <iostream>
>> #include <sstream>
>>
>> int main()
>> {
>>
>> // This yields 101010
>> std::cout << "The number 42 in binary: " << std::bin << 42 << '\n';
>>
>> }
>>
>>
>> It would also take advantage of other manipulators, like std::setw and
std::setfill:
>>
>>
>> #include <iostream>
>> #include <sstream>
>>
>> int main()
>> {
>>
>> // This yields 00101010
>>
>> std::cout << std::setw(8) << std::setfill('0');
>>
>> std::cout << "The number 42 in binary: " << std::bin << 42 << '\n';
>>
>> }
>>
>>
>> Reading binary numbers would also be addressed by the input streams.
>>
>>
>> IF this proposal is considered, I see changes in:
>>
>> - A new bin flag in ios_base::basefield.
>>
>> - Support for the flag in ios_base::setf, ios_base::unsetf and
ios_base::flags member functions.
>>
>> - Creation of the manipulator (similar to other manipulators) for the
different char_traits
>>
>> - Support for binary parsing for input streams
>>
>> - Support for binary output for ostreams.
>>
>>
>> std::ios_base in cppreference.com:
>>
>> https://en.cppreference.com/w/cpp/io/ios_base
>>
>>
>> Integer I/O manipulators
>>
>> https://en.cppreference.com/w/cpp/io/manip/hex
>>
>>
>> Regards,
>>
>> —Javier
>>
>>
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-02-11 06:28:43