Notes from C++ static analysis

monarch_dodra monarchdodra at gmail.com
Thu Jun 27 00:57:59 PDT 2013


On Thursday, 27 June 2013 at 07:33:16 UTC, Paulo Pinto wrote:
> On Wednesday, 26 June 2013 at 22:56:41 UTC, Walter Bright wrote:
>> On 6/26/2013 2:47 PM, Paulo Pinto wrote:
>>> I have been an adept of iostreams since day one and never 
>>> understood why people
>>> complain so much about them or the operator<< and operator>>
>>> for that matter.
>>
>> Even if you can get past the execrable look of it, it suffers 
>> from at least 3 terrible technical problems:
>>
>> 1. not thread safe
>>
>> 2. not exception safe
>>
>> 3. having to acquire/release mutexes for every << operation 
>> rather than once for the whole expression
>
> Thanks for listing those issues they actually represent areas 
> where I never used iostreams directly.
>
> 1. our iostream usage tends to be done from a central place
>
> 2. I am yet to write portable C++ code with exceptions turned on
>
> 3. wasn't aware of it

I *used* to like c++'s streams. Very recently, I did a project 
with high input output (not in terms of speed, but in terms of 
complexity), and it was *horrible!*

Streams have over fprints the ability to statically extract type, 
which is a win. However, it doesn't have format strings: What it 
has is global state, where it says "now I'm printing in hex", 
"now I'm printing with '0' as filler". "My next print will have 6 
width"!

Long story short, to print a 2-width '0' padded hex, followed by 
a 4 width ' ' padded decimal takes some 120 characters, 
completely obliterating what the original string was about. 
Trying to re-read the damn thing is near impossible. If you are 
serious about your stream manipulation, it also means you should 
save the state of your stream before each write, and then restore 
it at the end of your write (or in your catch...)

IMO, the concept of manipulators and global state is plain 
retarded. It would have been better to have a stormat *object*, 
that references the object that needs to be passed, and then the 
stream handles the formated object printing. EG:

std::cout << "0x" << std::format(5).w(2).f('0').hex() << 
std::endl;

It is still a bit verbose, but very less so, and much less 
intrusive, and it keeps the formats tied to the object, rather 
than the stream.

Fun fact, this should actually be pretty easy to implement :D...


More information about the Digitalmars-d mailing list