I’m going to be upfront.

I may a bit biased when it comes to iostream.

I think it is irredeemably bad, there’s nothing good about it, I hate it to the core, and I rather see it replaced.

And extending it, feels to very much like a wasted effort.

 

I think is far more productive in the long run to have separate things that deal only with formatting (and extend that) and create an independent file or i/o interface that just does reads and writes without formatting.

 

 

From: Std-Proposals <std-proposals-bounces@lists.isocpp.org> On Behalf Of Javier Estrada via Std-Proposals
Sent: Monday, February 10, 2025 7:01 PM
To: std-proposals@lists.isocpp.org
Cc: Javier Estrada <iphone.javier.estrada@gmail.com>
Subject: [std-proposals] std::bin manipulator for integer binary input/output

 

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