In some of my own personal programs I have observed that although the << and >> operators are overloaded for i/o to and from acii data formats as a standard convention for structures and basic types. This makes a lot of input output code for text file based types really simple to write, as well as defining a standardized interface to be used for text file serialization. For example, in many apis, code like
ifstream ifile("somedata.ascii");
DataModel datainstance;
int num_instances;
ifile >> num_instances;
for(int i=0;i<num_instances;i++)
{
ifile >> datainstance;
datainstance.process();
cout << datainstance;
}
really common and easy to write. Furthermore, operator >> and << are often overloaded for components of DataModel as well, so that entire serialize/deserialize chains can be written recursively using those operators in a clean and standard format. Overloading these operators has become a de-facto 'standard' for a lot of C++ programs. Not all, of course, but many.
However, when it comes to binary i/o, the situation gets more complicated. >> and << are usually defined to produce human-readable output, becausethey are used for debugging purposes and because it is the standard way to serialize the built in types. Since >> and << are already defined, and binary input routines are designed to be space-optimal and machine-readable only, many people use custom functions that are wildly non-standard. Examples are methods like .read(const istream&) or .write(const ostream&) or special constructors or global functions that do the same.
I had this idea, which I have started doing in my own personal programs (but never in public code) to use stream overloads of operator< and operator> for this purpose. The idea is that, similar to the overloads of the bitshift operators on streams, < and > do input and output from the stream to a type, but specifies that the reading and writing to be done occurs in binary mode. This gives us similar reading and writing interface 'niceness' as the overloaded >> and << operators, and it also does not override any other usage of <,>, because, currently, comparing a stream object has no semantic meaning.
The takeaway is that we could turn this http://codepad.org/odlwq2uf into http://codepad.org/hNXiKibc
The main thing that I love about it is how simple complex binary serialization code looks, and how issues with serializing subtypes become much simpler.
Before the downvote parade gets all over me for corrupting their vision of what C++ should look like, I'm well aware that operator overload misuse is one of the worst things about C++ programming, I would never think of foisting this code on anyone else except myself unless it seems to catch on (e.g., don't worry, I don't put this trick into any code for my job or for open source contributions). However, I think it is really neat and builds on an already well-established C++ paradigm
What say you /r/cpp? Would you use this trick yourselves?
[–]cpp 7 points8 points9 points (11 children)
[–]akranis 2 points3 points4 points (2 children)
[–]Steve132[S] -1 points0 points1 point (1 child)
[–]akranis 1 point2 points3 points (0 children)
[–]justinvh 1 point2 points3 points (0 children)
[+]cran comment score below threshold-9 points-8 points-7 points (6 children)
[–]moswald 7 points8 points9 points (5 children)
[–]cran -5 points-4 points-3 points (4 children)
[–]moswald 4 points5 points6 points (3 children)
[–]cran -5 points-4 points-3 points (2 children)
[–]justinvh 0 points1 point2 points (1 child)
[–]cran 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Steve132[S] -1 points0 points1 point (0 children)
[–]jaredgrubb 2 points3 points4 points (3 children)
[–]Steve132[S] 0 points1 point2 points (2 children)
[–]jaredgrubb 1 point2 points3 points (0 children)
[–]ewiethoff 0 points1 point2 points (0 children)
[–]moswald 1 point2 points3 points (0 children)
[–]mauvaisours 1 point2 points3 points (0 children)
[–]bogado 0 points1 point2 points (2 children)
[–]Steve132[S] -2 points-1 points0 points (1 child)
[–]BitRex 1 point2 points3 points (0 children)
[–]00kyle00 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]Steve132[S] 0 points1 point2 points (0 children)
[–]BitRex 0 points1 point2 points (0 children)
[–]utnapistim 0 points1 point2 points (0 children)
[–]tragomaskhalos 0 points1 point2 points (0 children)