Friday, September 18, 2020

Digest for comp.lang.c++@googlegroups.com - 3 updates in 1 topic

Juha Nieminen <nospam@thanks.invalid>: Sep 18 07:41PM


> I thought we were talking about formatted output, that's what fprintf()
> and streams are all about. I'm curious, how many times have you used
> std::ostream::write() in your operator<< ?
 
My point is that in the implementation of the operator<<() overload
for your custom type you can use std::ostream::write() to output the
value you want if you so want. Or any of the other functions that
std::ostream offers. (In some cases the write() function could
actually be the *simplest* way of outputing the value. It's also
probably the most efficient, as a bonus.)
 
You don't have that option in the scenario you suggested.
 
> Also, I consider it a good thing to modularize the program. Why should
> the code formatting the object content know or care about streams or
> FILE* pointers?
 
For optimization reasons.
 
And it's not like a stream-aware printing function and a generic
print-to-string function are mutually exclusive.
 
>> Also, somewhat ironically, in some cases you would need to resort to
>> stringstreams in order to create the string to be printed.
 
> So what? This is an implementation detail.
 
I just said it's a bit ironic.
 
 
> How comes? If everything supports conversion to strings, it's trivial to
> use in templates. Since C++11 we also have std::to_string() for
> primitive types, so they could be easily incorporated as well if needed.
 
std::to_string() does not support *any* formatting parameters
(such as printing in hex, or with a minimum width, with or without
leading characters, or the different types of floating point
formats, and so on.) It also doesn't support all primitive types,
most particularly pointers.
 
With std::ostream using formatting parameters may not be as handy
as with std::printf(), but at least the possibility is there.
Juha Nieminen <nospam@thanks.invalid>: Sep 18 07:45PM

> Isn't it that printf can be now implemented in type safe manner?
 
Not exactly printf, but a very printf-like function yes, indeed.
 
https://en.cppreference.com/w/cpp/utility/format
Paavo Helde <eesnimi@osa.pri.ee>: Sep 19 12:40AM +0300

18.09.2020 22:41 Juha Nieminen kirjutas:
> std::ostream offers. (In some cases the write() function could
> actually be the *simplest* way of outputing the value. It's also
> probably the most efficient, as a bonus.)
 
Well, I find it ironic that you bring up efficiency in the context of
iostreams, which are for sure the most inefficient way to output
something from a C++ program.
 
Efficiency is not always the most dominant concern, but when it is you
can bet iostreams are not the solution you are looking for. Just do a
little benchmark and measure string concatenation speed with
stringstream << vs std::string +=. Granted, std::string+= does not do
things like locale support, but when you are concerned about the speed
of output it means no human will look at the output and therefore no
locale-specific behavior is needed or wanted.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: