Tuesday, March 10, 2015

Digest for comp.lang.c++@googlegroups.com - 16 updates in 4 topics

ram@zedat.fu-berlin.de (Stefan Ram): Mar 06 12:05AM

Sorry, I have tried to search for »scope« in the C++
specification, but there are very many hits, and I gave up!
 
Maybe some reader already knows the answer:
 
It seems that the scope of »i« begins at the place
of the comment in the following program.
 
constexpr int f( int const * const p ){ return 2; }
int main(){ constexpr int i/**/{ f( &i ) }; }
 
Now, I would like to know the location in the C++
specification where this is specified.
ram@zedat.fu-berlin.de (Stefan Ram): Mar 05 11:19PM

>I know there exists a way to copy a filestream into a string in one
>line
 
If you take the required #include lines into account, it's
not possible, because every #include line must be written
on its own line.
 
Otherwise, it's simple, because the rest of the program
can always be written in a single line (assuming it does
not include preprocessor directives).
ram@zedat.fu-berlin.de (Stefan Ram): Mar 10 11:22PM

>What is a class, then?
 
»A class is a user-defined type.« - Bjarne Stroustrup
 
>I think you really mean "At what point should I create a new, distinct
>type for this thing I'm modelling, rather than let it join and mingle
>with the other happy integers?"
 
You seem to agree with Bjarne!
 
>I find that untrue. One of the eye-openers for me over the years has
>been how useful it is to make up distinct types for things which could
>have been represented as an int. And how few the drawbacks are.
 
int main()
{ int x;
^ Warning: Raw int used. (Raw ints will be deprecated in C++2z.)
Jorgen Grahn <grahn+nntp@snipabacken.se>: Mar 10 05:39PM

On Sun, 2015-03-08, David Brown wrote:
>> is, so you need to have it and be able to navigate it.
 
> I have never found "info" pages to be any use. The gcc manuals are fine:
 
> <https://gcc.gnu.org/onlinedocs/>
 
Aren't those the same thing as the info pages, though?
 
> Use the online html, html tarballs, or pdf files according to preference.
 
It's an individual preference. I hate info, but at least I can use it
within Emacs which I'm using /anyway/ ... and the HTML stuff inherits
some of the bad aspects of the info documentation so you suffer from
it, to some degree, in either case.
 
And the main point was "there's important documentation for GCC which
isn't in the man pages".
 
> gcc also comes with a fairly good built in help (with "--help -v" as a
> starting point) - recent versions let you get details of different
> groups of command-line switches.
 
Never tried it. But I should add that I looked at the man page now,
and it was better than I remembered it -- at least command-line
options and environment variables were properly documented, not just
listed with no explanation like I remembered them.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
David Brown <david.brown@hesbynett.no>: Mar 10 10:21PM +0100

On 10/03/15 18:39, Jorgen Grahn wrote:
 
>> I have never found "info" pages to be any use. The gcc manuals are fine:
 
>> <https://gcc.gnu.org/onlinedocs/>
 
> Aren't those the same thing as the info pages, though?
 
It is quite possible that they are the same - I don't find info pages
useful because the format is terrible and navigation is difficult, not
because the information contained is bad. HTML format is great for
jumping around, and for having multiple tabs with different parts of the
documentation open at once. PDF format is great when you want to read
through documentation like a book, search the whole thing, or print out
pages. I therefore have not bothered looking at the gcc info pages.
 
> within Emacs which I'm using /anyway/ ... and the HTML stuff inherits
> some of the bad aspects of the info documentation so you suffer from
> it, to some degree, in either case.
 
Indeed it is a matter of personal preference - I guess /some/ people
must like info!
 
However, /you/ were the one who said you didn't like info, but felt you
had to use it to get the information you needed - I was just trying to
be helpful by recommending alternative formats with (apparently) the
same information.
 
> and it was better than I remembered it -- at least command-line
> options and environment variables were properly documented, not just
> listed with no explanation like I remembered them.
 
The man pages and the built-in help have all got very much better in
recent versions of gcc.
Jorgen Grahn <grahn+nntp@snipabacken.se>: Mar 10 10:02PM

On Tue, 2015-03-10, David Brown wrote:
> On 10/03/15 18:39, Jorgen Grahn wrote:
>> On Sun, 2015-03-08, David Brown wrote:
...
 
>> it, to some degree, in either case.
 
> Indeed it is a matter of personal preference - I guess /some/ people
> must like info!
 
At least Stallman himself.
 
> had to use it to get the information you needed - I was just trying to
> be helpful by recommending alternative formats with (apparently) the
> same information.
 
Yes, and I kind of understood that.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Robert Wessel <robertwessel2@yahoo.com>: Mar 05 09:33PM -0600

>> 969 years.
 
>Oldest human was Methuselah. They knew how to take
>care of themselves and others.
 
 
Even if you make the (rather big) assumption that the bible has
historical accuracy, it does not assert that Methuselah was the oldest
human, but he is the oldest it mentions, and perhaps the oldest in the
genealogy leading to Noah, although that's still something of a
stretch.
David Brown <david.brown@hesbynett.no>: Mar 06 09:49AM +0100

>> genealogy leading to Noah, although that's still something of a
>> stretch.
 
> OK, oldest documented human was Methuselah.
 
There were equally well documented Sumerian kings who ruled for tens of
thousands of years each. The authors of the books of Moses took a lot
of inspiration from Sumerian stories and legends - they probably copied
the habit of exaggerating lifespans too.
 
Other old documented humans include some ancient Persian kings, as well
as many characters from India and the far east.
 
Remember, "documented" does not mean "true" !
Jorgen Grahn <grahn+nntp@snipabacken.se>: Mar 10 06:17PM

On Wed, 2015-03-04, Geoff wrote:
 
> The question you seem to be asking, in a long and tortuous way, is:
> At what point does an object become complex enough to deserve being a
> class?
 
What is a class, then? They are all types: structs/classes, various
built-in types, enums (old and new) ...
 
I think you really mean "At what point should I create a new, distinct
type for this thing I'm modelling, rather than let it join and mingle
with the other happy integers?"
 
> Answer: it depends on the abstraction of the object and the design
> goals for that object.
 
You mean, I think, "object" in the sense "some concept I have to
manage in my code", like age or coordinate.
 
Then I agree.
 
> probably not complex enough to deserve a class or the usage of the
> object would be more cumbersome as a class that it is as a naked
> object.
 
I find that untrue. One of the eye-openers for me over the years has
been how useful it is to make up distinct types for things which could
have been represented as an int. And how few the drawbacks are.
 
- You can document the type.
- You can restrict operations to things which make sense.
- You can avoid accidentally mixing it with other integers.
- You can overload on the type.
- You can select how it's formatted on a std::ostream.
- etc
 
In general, if you think of something as a distinct thing, it probably
deserves its own type. That's what they're there for, and that's what
C++ is really good at.
 
For example, I once maintained a code base with something like
 
struct Statistics {
unsigned long rx;
unsigned long tx;
unsigned rx_err;
// a dozen more
};
 
We constantly forgot to initialize them all to zero in the constructor,
until I realized that the right thing to do was to create an
Accumulator<T> which initialized itself to 0, and only supported
operator++() and printing itself.
 
Then there was the incident with the sequence numbers (my side, the
remote side's, the data plane's) which were mixed up in one odd code
path, and almost sent me to India to troubleshoot. Next time, I
created types LocalSeqno, RemoteSeqno, ... and that bug became
impossible.
 
> sex, GPA, ID_Number, etc.
 
> It's a design choice that is dictated by the scope of the program and
> it's goals.
 
That contradicts your "one wouldn't write a class for unsigned int
age". If ages are important to the problem you're solving, make Age a
type. If it's just an unimportant property of a Student and not
really used elsewhere, keep it as an int.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Victor Bazarov <v.bazarov@comcast.invalid>: Mar 05 05:26PM -0500

On 3/5/2015 5:17 PM, Christopher Pisz wrote:
> Method 2 :0.0216563
 
> That's quite a difference! What's going on under the hood with the
> iterator method?
 
I don't see any proof of the equality of the result of two different
methods...
 
Inserting into a text string, one character at a time, at the beginning,
most likely involves too many reallocations and too many copy
operations. Have you tried profiling your program?
 
V
--
I do not respond to top-posted replies, please don't ask
Christopher Pisz <nospam@notanaddress.com>: Mar 05 05:14PM -0600

On 3/5/2015 4:47 PM, Victor Bazarov wrote:
 
> Still, the only way to know for sure why the iterator method is slower
> is to profile it.
 
> V
 
 
I am not sure how to go about profiling it any deeper.
 
I'd have to start the research and purchase of a native C++ profiler
campaign up again. The last one ended in failure.
 
 
Supposedly the Windows SDK comes with Xperf according to Google and that
can be used. I don't know where to start, but its worth learning at this
point.
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
Christopher Pisz <nospam@notanaddress.com>: Mar 05 05:26PM -0600

On 3/5/2015 5:19 PM, Stefan Ram wrote:
 
> Otherwise, it's simple, because the rest of the program
> can always be written in a single line (assuming it does
> not include preprocessor directives).
 
You a funny guy!
 
Statement. One statement.
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
Christopher Pisz <nospam@notanaddress.com>: Mar 05 07:11PM -0600

On 3/5/2015 4:47 PM, Victor Bazarov wrote:
 
> Still, the only way to know for sure why the iterator method is slower
> is to profile it.
 
> V
 
 
So, I got Xperf to work after watching a few hours of videos and working
through a lot of out of date information.
 
It reports that
Method 1 has 44 heap allocations for a size of 217,357.
Method 2 has 26 heap allocations for a size of 194,437.
 
I rely on my high precision timer for the time of execution for both.
 
Method 1 :0.283209
Method 2 :0.0216563
 
I can't really find anything about istream_iterator or std::inserter
though. I like the code in method 1, it feels cleaner. I want to
understand if there is another step I am missing or something else I can
do similar.
 
It is fairly often I'd want to copy the contents of one stream to a
string or to another stream.
 
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
Victor Bazarov <v.bazarov@comcast.invalid>: Mar 05 05:47PM -0500

On 3/5/2015 5:26 PM, Victor Bazarov wrote:
 
> Inserting into a text string, one character at a time, at the beginning,
> most likely involves too many reallocations and too many copy
> operations.
 
OK, I take it back. I obviously don't know how 'std::inserter' works.
 
> Have you tried profiling your program?
 
Still, the only way to know for sure why the iterator method is slower
is to profile it.
 
V
--
I do not respond to top-posted replies, please don't ask
Paavo Helde <myfirstname@osa.pri.ee>: Mar 05 11:55PM -0600

Christopher Pisz <nospam@notanaddress.com> wrote in
> Method 2 :0.0216563
 
> That's quite a difference! What's going on under the hood with the
> iterator method?
 
The stream classes are meant for reading and writing *formatted* text at
high level (streaming a sequence of C++ objects of arbitrary classes to
and from a file). The current locale is taken into account on every step,
end-of-line conversions done, etc. The iterator works in this layer even
if it actually does no transformations. I would not be surprised if there
were a couple of virtual function calls involved with reading each
character.
 
OTOH, by using rdbuf() you actually say that you do not want all that
formatting and translation and want just to get the file content as it is
on the disk. Naturally this is a lot faster.
 
Still, I would expect a difference of at most 3-4 times, the stream
abstractions are not that bad. Looks like you are testing non-optimized
code. Also, the times like 0.02 seconds are too close to the OS process
scheduling granularity, plus the effects from OS file caching may show
up. I would put some loops in main() to get larger times, and the very
first runs of both methods should be ignored.
 
hth
Paavo
Juha Nieminen <nospam@thanks.invalid>: Mar 06 08:19AM

> std::copy(std::istream_iterator<char>(file),
> std::istream_iterator<char>(),
> std::inserter(textToParse, textToParse.begin()));
 
What's the problem with
 
std::string textToParse(std::istream_iterator<char>(file),
std::istream_iterator<char>());
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
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: