Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Learning modern C++, where to start?

273 views
Skip to first unread message

Unto Sten

unread,
Jan 5, 2019, 5:32:10 PM1/5/19
to
Hello!

Years ago, I read Bjärne's C++ Book, third edition.
I never did any real C++ programming, but I have
read some C++ source, and am familiar with most of the
core concepts.

Since then, C++ Book 4th edition has appeared,
covering C++11.

But after that we have C++14 and Bjärne says in:

http://www.stroustrup.com/C++11FAQ.html

####################################################
Will there be a C++1y?

Almost certainly -- and not just because the committee
has slipped the deadline for C++0x. The plans for minor
revisions, C++14, are well advanced (the features have
been voted into the working draft and implemented), and
the plan is for a major revision in 2017, C++17.
####################################################

Bjärne says C++11 feels "like a new language" and
yet C++17 is planned to be "a major revision"...?

Yes, C++17 materialized:

https://en.wikipedia.org/wiki/C%2B%2B17

According to Wikipedia, the next standard is going to
be C++20.

I do not mean to be trolling, but I would like
to take the time learn C++ properly, since
incorrect C++ usage seems to me as dangerous as
incorrect usage of C. So many chances to create
nasty bugs when falling victim to undefined behaviour
and so on.

But this C++ language seems to grow and grow, and
new standards arrive frequently. I am pretty confused.

What on earth should one do to learn this language?
Yeah, they say most C++ projects use only a subset
of C++ features, but those subsets can be different for
different groups - forcing you to learn a lot.

Quite frankly, I am going crazy here, not knowing
how to start. Yeah, I know OOP pretty well, but I
am thinking of all those C++ details that you have
to master...

I guess I should buy C++ Book 4th edition, but I
am afraid those new standards make some features
outdated, and introduce new ways of doing things.

Best regards,
Unto Sten

Ian Collins

unread,
Jan 5, 2019, 5:57:10 PM1/5/19
to
Yes, buy the 4th edition, it is a good starting point. The changes
between the original C++ and C++11 where the most significant to date.
C++14 was a mopping up exercise and C++17 brings in some new stuff but
the changes are nowhere near as significant as those in C++11.

Once you have a foothold, learn by doing!

--
Ian.

Unto Sten

unread,
Jan 5, 2019, 6:15:17 PM1/5/19
to
Ian Collins <ian-...@hotmail.com> wrote:
> Once you have a foothold, learn by doing!

Thanks for the information. I will buy Bjärne's
C++ Book, 4th edition.

You are right, it is not possible to learn C++ just
by reading books and other people's code. You have
to write real programs, there is no other way to do
it.

Best regards,
Unto Sten

TYSC++

unread,
Jan 5, 2019, 6:19:20 PM1/5/19
to
On 05/01/2019 22:32, Unto Sten wrote:
>
> I do not mean to be trolling, but I would like
> to take the time learn C++ properly, since
> incorrect C++ usage seems to me as dangerous as
> incorrect usage of C. So many chances to create
> nasty bugs when falling victim to undefined behaviour
> and so on.
>
>

C++ in One Hour a Day, Sams Teach Yourself Paperback – 23 Dec 2016
Paperback: 798 pages
Publisher: Sams Publishing; 8 edition (23 Dec. 2016)
Language: English
ISBN-10: 0789757745
ISBN-13: 978-0789757746
Product Dimensions: 17.8 x 5 x 23.2 cm



--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

I set my girlfriend's panythose on fire

unread,
Jan 6, 2019, 9:10:19 AM1/6/19
to
On Saturday, January 5, 2019 at 3:19:20 PM UTC-8, TYSC++ wrote:
> On 05/01/2019 22:32, Unto Sten wrote:
> >
> > I do not mean to be trolling, but I would like
> > to take the time learn C++ properly, since
> > incorrect C++ usage seems to me as dangerous as
> > incorrect usage of C. So many chances to create
> > nasty bugs when falling victim to undefined behaviour
> > and so on.
> >
> >
>
> C++ in One Hour a Day, Sams Teach Yourself Paperback – 23 Dec 2016
> Paperback: 798 pages
> Publisher: Sams Publishing; 8 edition (23 Dec. 2016)
> Language: English
> ISBN-10: 0789757745
> ISBN-13: 978-0789757746
> Product Dimensions: 17.8 x 5 x 23.2 cm
>
>
>

Should I cite a former VP at google that says reading this book is a bad idea?

Daniel

unread,
Jan 6, 2019, 11:09:09 AM1/6/19
to
On Sunday, January 6, 2019 at 9:10:19 AM UTC-5, I set my girlfriend's panythose on fire wrote:
> On Saturday, January 5, 2019 at 3:19:20 PM UTC-8, TYSC++ wrote:

> > C++ in One Hour a Day, Sams Teach Yourself Paperback – 23 Dec 2016
>
> Should I cite a former VP at google that says reading this book is a bad
> idea?

No, that would be the "appeal to authority" fallacy.

Daniel

Siri Cruise

unread,
Jan 6, 2019, 12:38:55 PM1/6/19
to
With Pascal?

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|\
The first law of discordiamism: The more energy This post / \
to make order is nore energy made into entropy. insults Islam. Mohammed

Unto Sten

unread,
Jan 6, 2019, 2:00:38 PM1/6/19
to
I guess he could cite him or her, as long as it is not
a mere citation, but a coherent text containing the
*reasons* why the proposed book could be harmful.

(By the way, sorry for mispelling "Bjarne" as "Bjärne")

Best regards,
Unto Sten

cda...@gmail.com

unread,
Jan 6, 2019, 2:18:12 PM1/6/19
to
Well, okay, the guy doesn't cite this book. But he does criticize something that he feels falls within the same category.

Taken from his site at..

http://norvig.com/21-days.html

And I quote..

"
Walk into any bookstore, and you'll see how to Teach Yourself Java in 24 Hours alongside endless variations offering to teach C, SQL, Ruby, Algorithms, and so on in a few days or hours. The Amazon advanced search for [title: teach, yourself, hours, since: 2000 and found 512 such books. Of the top ten, nine are programming books (the other is about bookkeeping). Similar results come from replacing "teach yourself" with "learn" or "hours" with "days."
The conclusion is that either people are in a big rush to learn about programming, or that programming is somehow fabulously easier to learn than anything else. Felleisen et al. give a nod to this trend in their book How to Design Programs, when they say "Bad programming is easy. Idiots can learn it in 21 days, even if they are dummies." The Abtruse Goose comic also had their take.
"

And then he goes on to say..

"
Let's analyze what a title like Teach Yourself C++ in 24 Hours could mean:
Teach Yourself: In 24 hours you won't have time to write several significant programs, and learn from your successes and failures with them. You won't have time to work with an experienced programmer and understand what it is like to live in a C++ environment. In short, you won't have time to learn much. So the book can only be talking about a superficial familiarity, not a deep understanding. As Alexander Pope said, a little learning is a dangerous thing.

C++: In 24 hours you might be able to learn some of the syntax of C++ (if you already know another language), but you couldn't learn much about how to use the language. In short, if you were, say, a Basic programmer, you could learn to write programs in the style of Basic using C++ syntax, but you couldn't learn what C++ is actually good (and bad) for. So what's the point? Alan Perlis once said: "A language that doesn't affect the way you think about programming, is not worth knowing". One possible point is that you have to learn a tiny bit of C++ (or more likely, something like JavaScript or Processing) because you need to interface with an existing tool to accomplish a specific task. But then you're not learning how to program; you're learning to accomplish that task.

in 24 Hours: Unfortunately, this is not enough, as the next section shows.
"

Unto Sten

unread,
Jan 7, 2019, 10:51:30 AM1/7/19
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> And I quote from the same page
> (which, BTW, has my name on it):
>
> |So go ahead and buy that Java/Ruby/Javascript/PHP book;
> |you'll probably get some use out of it.

Sorry, Stefan, I do not get your point.

To be clear: Is the recommended book one of those bad
"Learn C++ in no time" hoaxes, or a proper textbook?

At least the length of it was something like 800 pages,
so it most definitely has some amount of content in it.

Regardless of the outcome, I think I am going to buy
Bjarne's book. And *if* the "Teach Yourself" is any good,
I just might get it too...

Best regards,
Unto Sten

Jorgen Grahn

unread,
Jan 7, 2019, 5:44:16 PM1/7/19
to
On Sat, 2019-01-05, Unto Sten wrote:
> Hello!
>
> Years ago, I read Bjärne's C++ Book, third edition.
> I never did any real C++ programming, but I have
> read some C++ source, and am familiar with most of the
> core concepts.
>
> Since then, C++ Book 4th edition has appeared,
> covering C++11.
>
> But after that we have C++14 and Bjärne says in:

Bjarne. There's no ä in the name (or in the language).

...
> I do not mean to be trolling, but I would like
> to take the time learn C++ properly, since
> incorrect C++ usage seems to me as dangerous as
> incorrect usage of C. So many chances to create
> nasty bugs when falling victim to undefined behaviour
> and so on.

Maybe I'm lazy, but from my point of view you can read the 3rd edition
and learn to write good C++. IMO, the line between old and crufty C++
and modern C++ is C++98, when the standard library containers appeared
and (years later) people started using them properly.

> But this C++ language seems to grow and grow, and
> new standards arrive frequently. I am pretty confused.

Some people like new, exciting things, and these are the people you
hear. But C++ is also a language for people like me, who are more
comfortable with tools they've used for many years.

I do use C++11 features (auto, ranged-for, uniform initializers,
override, some of the library additions ...), but they don't make the
code drastically better or different, and I didn't have to learn much
to use them.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Öö Tiib

unread,
Jan 8, 2019, 2:25:10 AM1/8/19
to
On Monday, 7 January 2019 17:51:30 UTC+2, Unto Sten wrote:
> Regardless of the outcome, I think I am going to buy
> Bjarne's book. And *if* the "Teach Yourself" is any good,
> I just might get it too...

You perhaps have already looked at that very popular
Stack Overflow question:
https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
The tastes vary but certainly the books that are
mentioned there are worth reading.

Manfred

unread,
Jan 8, 2019, 8:10:06 AM1/8/19
to
On 1/7/2019 11:44 PM, Jorgen Grahn wrote:
> On Sat, 2019-01-05, Unto Sten wrote:
>> Hello!
>>
>> Years ago, I read Bjärne's C++ Book, third edition.
>> I never did any real C++ programming, but I have
>> read some C++ source, and am familiar with most of the
>> core concepts.
>>
>> Since then, C++ Book 4th edition has appeared,
>> covering C++11.
>>
>> But after that we have C++14 and Bjärne says in:
>
> Bjarne. There's no ä in the name (or in the language).
>
> ...
>> I do not mean to be trolling, but I would like
>> to take the time learn C++ properly, since
>> incorrect C++ usage seems to me as dangerous as
>> incorrect usage of C. So many chances to create
>> nasty bugs when falling victim to undefined behaviour
>> and so on.
>
> Maybe I'm lazy, but from my point of view you can read the 3rd edition
> and learn to write good C++. IMO, the line between old and crufty C++
> and modern C++ is C++98, when the standard library containers appeared
> and (years later) people started using them properly.

This sounds a bit simplistic, I think. If C++98 was a major
breakthrough, C++11 was somewhat of a major revision too.
So, if one wishes to catch up today, I would recommend the 4th edition
to start with.
On top of this, a reasonable follow-up could be "Effective Modern C++"
by Scott Meyers.

>
>> But this C++ language seems to grow and grow, and
>> new standards arrive frequently. I am pretty confused.

I a no fan of ever-changing languages either. In fact I consider every
change to the language a cost, which is not always paid off by the
benefit of the new feature.
More precisely, I do not agree with those who claim that a new feature
is inexpensive if you don't want to use it - even if you don't use it,
you need to know about it, and this is not cost-free.

I agree with others that C++14 and C++17 are less big of a change wrt C++11.
C++20 is probably going to be another major change, especially in the
area of template metaprogramming, because of the introduction of
template argument requirements (aka C++ "concepts" - btw poor choice of
name IMVHO)

Besides changes in the language, in the standard library there have been
a lot of additions wrt C++98, like e.g. concurrency, resource management
({unique,shared,weak}_ptr etc.), traits, etc. which are worth knowing about.

An overview can be found in
https://en.cppreference.com/w/
with some indication of which C++ version introduced what.

Daniel

unread,
Jan 8, 2019, 10:26:43 AM1/8/19
to
On Tuesday, January 8, 2019 at 2:25:10 AM UTC-5, Öö Tiib wrote:
>
> You perhaps have already looked at that very popular
> Stack Overflow question:
> https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
> The tastes vary but certainly the books that are
> mentioned there are worth reading.

Just curious, does anyone here read the big tome books anymore, such as Lippman and Stroustrup? Does a book that purports to cover "just about everything in the language" have a place anymore?

At one time, I had an early edition of Lippman on my shelf, which I used as a reference. But nobody needs a book as a reference anymore. cppreference performs that role admirably.

As for programming techniques, take unicode. I did a search on unicode in my Stroustrup third edition, and find many sentences like "There are six kinds of character literals supporting Unicode" (Unicode literals.) This sounds excessive, but there are three major encodings of Unicode: UTF-8, UTF-16, and UTF-32. For each of these three alternatives, both raw and "ordinary" strings are supported." Etc. etc. etc. But nothing about emerging consensus in how to work with unicode in practical C++ applications.

If I were recommending a book from the list, I would probably pick Koenig's Accelerated C++, mostly because it's shorter. Somebody learning C++ doesn't need an encyclopedia, for what reason would they want an encyclopedia? But unfortunately it doesn't look like it's ever been updated.

Daniel

Unto Sten

unread,
Jan 8, 2019, 1:58:05 PM1/8/19
to
Öö Tiib <oot...@hot.ee> wrote:
> https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
> The tastes vary but certainly the books that are
> mentioned there are worth reading.

Thanks for the tip.

Best regards,
Unto Sten

Alf P. Steinbach

unread,
Jan 8, 2019, 2:53:09 PM1/8/19
to
On 08.01.2019 16:26, Daniel wrote:
> On Tuesday, January 8, 2019 at 2:25:10 AM UTC-5, Öö Tiib wrote:
>>
>> You perhaps have already looked at that very popular
>> Stack Overflow question:
>> https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
>> The tastes vary but certainly the books that are
>> mentioned there are worth reading.
>
> Just curious, does anyone here read the big tome books anymore, such as Lippman and Stroustrup? Does a book that purports to cover "just about everything in the language" have a place anymore?
>
> At one time, I had an early edition of Lippman on my shelf, which I used as a reference. But nobody needs a book as a reference anymore. cppreference performs that role admirably.
>
> As for programming techniques, take unicode. I did a search on unicode in my Stroustrup third edition, and find many sentences like "There are six kinds of character literals supporting Unicode" (Unicode literals.) This sounds excessive, but there are three major encodings of Unicode: UTF-8, UTF-16, and UTF-32. For each of these three alternatives, both raw and "ordinary" strings are supported." Etc. etc. etc. But nothing about emerging consensus in how to work with unicode in practical C++ applications.

Bjarne hasn't had time to do anything about it. Or hadn't, a year ago.

There is already an /established/ consensus for handling Unicode,
namely, use UTF-8 for all external text representation as well as
internal general handling, and convert to/from other UTF's e.g. for
using the ICU library, or the Windows API.

To use UTF-8 in simple Windows console applications in a portable way
you can use my Wrapped Stdlib header library.

Also, there was already the first stages of adoption of an UTF-8 i/o
library in Boost, but it required explicit use of its functionality (not
just ordinary iostreams i/o) and it didn't correctly handle line
termination in Windows, when I tried it.

However, the goal of Wrapped Stdlib was to introduce as /little/ as
possible that wasn't already in the standard library. It was a technical
corrigendum, so to speak. And I chose wrong: I chose to address some
compiler peculiarities, to provide automatic fixes, so that the novice
should not have to struggle with complicated technical issues just to
get up and running, but I should have known, from experience, that it's
impossible to keep up with Microsoft's sabotage-like steady stream of
new pecularities, e.g. diagnostics avalanches from perfectly valid and
good code, that one must opt out from by defining a macro...


> If I were recommending a book from the list, I would probably pick
Koenig's Accelerated C++, mostly because it's shorter. Somebody learning
C++ doesn't need an encyclopedia, for what reason would they want an
encyclopedia? But unfortunately it doesn't look like it's ever been updated.

Yup.


Cheers!,

- Alf

Bonita Montero

unread,
Jan 9, 2019, 4:12:55 AM1/9/19
to
Whatever you get told about learning the core-language:
You shoud read "The C++ Standard Library: A Tutorial and Reference"
from Josuttis also.

Öö Tiib

unread,
Jan 9, 2019, 10:35:29 AM1/9/19
to
Worst are silently breaking changes when some reasonable and
well-formed code is still well-formed but behaves differently.
Noisy breaking changes (like change of meaning of keyword
"auto" was) are lot easier to comply with. So indeed the issue is
that weak, rarely used or controversial features are very rarely
removed or straightened up and instead numerous of
semi-useful or loose things are added.

For example in C++17 most of changes that were done
were adding likely rarely helpful features:
* more kinds and more places for [[blah_blah]] attributes.
These are rarely used and are ugly.
* In template-template parameter typename is allowed as
synonym to class. More anarchy.
* auto deduction of braced-init list changed. That braced-
init-list was badly thought thru and who did auto-deduce
from it without desire to confuse others?
* structural auto binding of elements of arrays, tuple-likes
and structs. Expecting more meta-knowledge around
that "auto" usage.
* utf8 char literal does not do what one would expect.
* hexadecimal floating point literal is likely rarely useful.
* fold expressions for variadic templates, again rarely
needed syntax sugar.
* if constexpr ... disappointment, does not do what one
would expect.

There are some other, more useful features but only
few.

Jorgen Grahn

unread,
Jan 10, 2019, 12:12:13 PM1/10/19
to
On Tue, 2019-01-08, Manfred wrote:
> On 1/7/2019 11:44 PM, Jorgen Grahn wrote:
>> On Sat, 2019-01-05, Unto Sten wrote:
...
>>> I do not mean to be trolling, but I would like
>>> to take the time learn C++ properly, since
>>> incorrect C++ usage seems to me as dangerous as
>>> incorrect usage of C. So many chances to create
>>> nasty bugs when falling victim to undefined behaviour
>>> and so on.
>>
>> Maybe I'm lazy, but from my point of view you can read the 3rd edition
>> and learn to write good C++. IMO, the line between old and crufty C++
>> and modern C++ is C++98, when the standard library containers appeared
>> and (years later) people started using them properly.
>
> This sounds a bit simplistic, I think. If C++98 was a major
> breakthrough, C++11 was somewhat of a major revision too.

It's all rather subjective, but to me C++11 isn't /that/ big of a deal
since it didn't fundamentally change my designs. And regarding the
OP's "incorrect C++", my pre-C++11 code isn't suddenly incorrect, or
even archaic.

> So, if one wishes to catch up today, I would recommend the 4th edition
> to start with.

I agree. But at the same time, people should not avoid C++ because
it seems to be undergoing drastic changes.

...
>> I do use C++11 features (auto, ranged-for, uniform initializers,
>> override, some of the library additions ...)

I should have mentioned lambdas here, too. They are fairly easy to
understand, and make <algorithm> more useful in practice, among other
things.

Manfred

unread,
Jan 10, 2019, 2:32:05 PM1/10/19
to
On 1/10/2019 6:12 PM, Jorgen Grahn wrote:
> On Tue, 2019-01-08, Manfred wrote:
>> On 1/7/2019 11:44 PM, Jorgen Grahn wrote:
>>> On Sat, 2019-01-05, Unto Sten wrote:
> ...
>>>> I do not mean to be trolling, but I would like
>>>> to take the time learn C++ properly, since
>>>> incorrect C++ usage seems to me as dangerous as
>>>> incorrect usage of C. So many chances to create
>>>> nasty bugs when falling victim to undefined behaviour
>>>> and so on.
>>>
>>> Maybe I'm lazy, but from my point of view you can read the 3rd edition
>>> and learn to write good C++. IMO, the line between old and crufty C++
>>> and modern C++ is C++98, when the standard library containers appeared
>>> and (years later) people started using them properly.
>>
>> This sounds a bit simplistic, I think. If C++98 was a major
>> breakthrough, C++11 was somewhat of a major revision too.
>
> It's all rather subjective, but to me C++11 isn't /that/ big of a deal
> since it didn't fundamentally change my designs. And regarding the
> OP's "incorrect C++", my pre-C++11 code isn't suddenly incorrect, or
> even archaic.
One of the greatest features of C++ is its reliability, in the sense of
backward compatibility guarantee, which makes C++ code valid for a long
time - which makes it suitable for serious applications.
Still today, or even after C++20, you can write C-like or "C with
classes" C++ code (I mean it is possible, not that you actually do) and
rely that it will produce correct executables.

But even if C++11 did not let into obsolescence the billions of lines of
existing C++ code, still it introduced changes that I (and many others)
consider relevant enough to put the language into a new perspective.
Bjarne himself said it can almost be seen as an entirely new language.
From this point of view, the fact that "C with classes" code still is
valid C++ code is even more impressive, but does not change the fact.

Some examples: constexpr, traits and type support (enable_if), function
objects (including lambdas), move semantics, variadic templates,
standardized smart pointers ad so forth do place the language into a new
perspective, which is best learnt from start than caught up afterwards.

>
>> So, if one wishes to catch up today, I would recommend the 4th edition
>> to start with.
>
> I agree. But at the same time, people should not avoid C++ because
> it seems to be undergoing drastic changes.

Not at all. As I said, one of the greatest values of C++ is your code
will keep running for a very long time.
I, like many others, have quite some code from the '90s which is still
recompiled and run today.
By the way, Bjarne is aware of the risk of too much growth too - look
for "remember the Wasa"

>
> ...
>>> I do use C++11 features (auto, ranged-for, uniform initializers,
>>> override, some of the library additions ...)
>
> I should have mentioned lambdas here, too. They are fairly easy to
> understand, and make <algorithm> more useful in practice, among other
> things.

Good point that you mention "easy to understand" features. I think the
most effective improvements in C++ are those that even when opening wide
new perspectives, still are essential and thus easy to grasp. This adds
to their effectiveness.

>
> /Jorgen
>

Unto Sten

unread,
Jan 11, 2019, 11:57:51 AM1/11/19
to
Jorgen Grahn <grahn...@snipabacken.se> wrote:
> I should have mentioned lambdas here, too. They are fairly easy to
> understand, and make <algorithm> more useful in practice, among other
> things.

Yes, the lambda functions are easy to understand at least in the
languages that I know. And I guess the meaning of "lambda"
is exactly the same in C++ too.

For some reason, people who love the functional programming
paradigm, are overjoyed with lambda functions. I am not sure
why - it is just a way create a function in the calling site
and the function has no name. :-D

Best regards,
Unto Sten

Paavo Helde

unread,
Jan 11, 2019, 4:34:12 PM1/11/19
to
Well, it seems you have missed the point that a lambda does not create a
function, but rather a functor with a potentially rich data state.

This is the same difference what a class has over a free function, so in
some sense similar to one of the key differences of C and C++. OTOH,
there are many people who think C and C++ are the same language, so
maybe that's not so big deal as I think...

Unto Sten

unread,
Jan 12, 2019, 10:26:42 AM1/12/19
to
Paavo Helde <myfir...@osa.pri.ee> wrote:
> Well, it seems you have missed the point that a lambda does not create a
> function, but rather a functor with a potentially rich data state.

Okay, I have to study more about lambdas. Could you refer to some
C++ example code that demonstrates the functor property of lambda
functions?

> This is the same difference what a class has over a free function, so in
> some sense similar to one of the key differences of C and C++.

I see.

> OTOH, there are many people who think C and C++ are the same
> language, so maybe that's not so big deal as I think...

Yes, it is admittedly pretty annoying to read job requirements such as:

* need to know C/C++

Of course C language is *almost* a subset of C++, and in that sense
C counts as valid C++ as well, but the differences are huge. So I think
that the expression "C/C++" is very confusing.

Best regards,
Unto Sten

Unto Sten

unread,
Jan 12, 2019, 12:35:11 PM1/12/19
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> He might refer to the lambda being a closure,
> i.e., having bindings to its environment.

Yes, that's what I thought, too. I have seen closure
examples using lambda functions, but have been unable
to see what's the actual benefit of using closures in
the first place.

The following article provides some insight using JavaScript,
but I am still a bit shaky about this:

https://medium.com/@dis_is_patrick/practical-uses-for-closures-c65640ae7304

Best regards,
Unto Sten

Paavo Helde

unread,
Jan 12, 2019, 3:50:42 PM1/12/19
to
On 12.01.2019 17:26, Unto Sten wrote:
> Paavo Helde <myfir...@osa.pri.ee> wrote:
>> Well, it seems you have missed the point that a lambda does not create a
>> function, but rather a functor with a potentially rich data state.
>
> Okay, I have to study more about lambdas. Could you refer to some
> C++ example code that demonstrates the functor property of lambda
> functions?

The simplest example: find an element in a vector not equal to x.

int x = 42;
std::vector<int> v= ...;
auto p = std::find_if(v.begin(), v.end(), [x](int y){return x!=y;});

Note passing the data to lambda via [x]. If it were [&x] then it would
just store a reference to the original x. This would make a difference
if the lambda is passed over to another thread or stored for later use,
for example. Note that std::find_if does not know or care about x, it
just (potentially) copies and invokes the lambda.

An extract from real production code:

// Parallelize over 2D planes of a 3D array
CubeParallelizer2 parallelizer(ctx.GetEngine(), dims);
parallelizer.PlaneLoop(ctx,
[this, dims, &sourcePlanePointers, &maskPlanePointers, &pHess1,
&pHess2, &pHess3, &pNegTrace, sourceType, resultType]
(dimsize_t z)
{
Run3DPlane(
z > 0 ? sourcePlanePointers[z - 1] : nullptr,
sourcePlanePointers[z],
z < dims[2] - 1 ? sourcePlanePointers[z + 1] : nullptr,
dims[0],
dims[1],
maskPlanePointers[z],
pHess1[z],
pHess2[z],
pHess3[z],
pNegTrace[z],
sourceType,
resultType
);
});

Here a lambda is passed over to parallel worker threads, with some bound
data passed by reference and some by value. Note that the parallelizing
infrastructure (parallelizer.PlaneLoop()) does not know or care about
the bound data, it just copied and invokes the lambda. Note also how the
bound data and the explicit parameter z are combined for selecting the
data to use in the other thread.




Unto Sten

unread,
Jan 13, 2019, 1:09:08 AM1/13/19
to
Paavo Helde <myfir...@osa.pri.ee> wrote:
> The simplest example: find an element in a vector not equal to x.
>
> int x = 42;
> std::vector<int> v= ...;
> auto p = std::find_if(v.begin(), v.end(), [x](int y){return x!=y;});
>
> Note passing the data to lambda via [x]. If it were [&x] then it would
> just store a reference to the original x. This would make a difference
> if the lambda is passed over to another thread or stored for later use,
> for example. Note that std::find_if does not know or care about x, it
> just (potentially) copies and invokes the lambda.

Okay, thanks for this. I will save the whole discussion thread and
get back to it later.

> An extract from real production code: [...]

I am hopelessly bad with 3D math, so I do not understand the code,
but I am sure it is helpful to those who get the 3D stuff.

Best regards,
Unto Sten

Chris Vine

unread,
Jan 13, 2019, 2:49:56 PM1/13/19
to
On Sat, 12 Jan 2019 17:34:59 -0000 (UTC)
sten...@gmail.com (Unto Sten) wrote:
> Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> > He might refer to the lambda being a closure,
> > i.e., having bindings to its environment.
>
> Yes, that's what I thought, too. I have seen closure
> examples using lambda functions, but have been unable
> to see what's the actual benefit of using closures in
> the first place.

One common use of closures is for partial application (which you can
google for in your own time), converting a function taking some
arguments into a function taking less or no arguments. (A value bound
or captured from the environment by a closure is somewhat confusingly
called a "free variable" in the lambda calculus.)

In C++ std::bind can also be used to construct closures, and you can
read up on that too.

But don't underestimate the usefulness of lambda expressions for other
purposes, particularly as temporary unnamed function objects. They can
be very useful for passing "on the fly" to functions taking callable
objects - the algorithms in the standard library have many such
functions. It makes code much more readable when done properly.

seeplus

unread,
Jan 14, 2019, 1:08:47 AM1/14/19
to
You might like to look at this book at Packt publishing

Modern C++: Efficient and
Scalable Application
Development

Richard Grimes, Marius Bancila

It is published Nov 18 so uptodate ... for now.
They have joined together some other books.
Fairly well known authors and reviewers.
Quite a good writing style and it covers just about everything. 675pp.

*** Best of all, currently on sale for US$5 for pdf. Cup of coffee.

Books can sometimes be better than using on line info.
You can see the whole of C++ at one glance and pick out stuff
that you don't know, or find things you could/should use.
Good for plane, train, bus or where no web access.

Of course opinions do differ on the language, and
all big books get errata. There is none listed at present.

Alf P. Steinbach

unread,
Jan 14, 2019, 6:17:41 PM1/14/19
to
On 12.01.2019 16:26, Unto Sten wrote:
> Paavo Helde <myfir...@osa.pri.ee> wrote:
>> Well, it seems you have missed the point that a lambda does not create a
>> function, but rather a functor with a potentially rich data state.
>
> Okay, I have to study more about lambdas. Could you refer to some
> C++ example code that demonstrates the functor property of lambda
> functions?

The following code aids in unwrapping the exception messages of a C++11
exception that can contain a nested exception that can contain..., and
so on. In this code cppx::C_str is a `typedef` of `char const*`. Usually
it's ungood to name pointer types but I've found this one useful, so.


------------------------------------------------------------------------
#pragma once // Source encoding: UTF-8 with BOM (π is a lowercase
Greek "pi").

#include <cppx-core/collections/is_empty.hpp> //
cppx::is_empty
#include <cppx-core/core-language/$use_from_namespace.hpp> // CPPX_USE_STD
#include <cppx-core/text/C_str_.hpp> // cppx::C_str

#include <exception> // std::(exception, rethrow_exception)
#include <functional> // std::function
#include <utility> // std::move

namespace cppx
{
CPPX_USE_STD( exception, function, move, rethrow_if_nested, string );

inline void call_with_description_lines_from(
const exception& x,
const function<void( const C_str )>& f
)
{
f( x.what() );
try
{
rethrow_if_nested( x );
}
catch( const exception& rx )
{
call_with_description_lines_from( rx, f );
}
catch( ... )
{
f( "<a non-standard exception>" );
}
}

inline auto description_lines_from( const exception& x )
-> string
{
string result;
const auto add = [&]( const C_str s ) -> void
{
if( not is_empty( result ) )
{
result += '\n';
}
result += s;
};
call_with_description_lines_from( x, add );
return result;
}

} // namespace cppx
------------------------------------------------------------------------


Here the lambda in `description_lines_from` adds lines of text to the
local variable in that function, `result`.


[snip]


Cheers!,

- Alf

Paavo Helde

unread,
Jan 15, 2019, 3:17:43 PM1/15/19
to
On 13.01.2019 8:08, Unto Sten wrote:
> Paavo Helde <myfir...@osa.pri.ee> wrote:
>
>> An extract from real production code: [...]
>
> I am hopelessly bad with 3D math, so I do not understand the code,
> but I am sure it is helpful to those who get the 3D stuff.

This was just a highly illustrative example about transferring data in a
lambda, nothing to do with 3D algorithms, you are not supposed to
understand what it actually does, this would require much larger context
and much more time.

The only point of interest was that all the various data passed to
lambda in the brackets [] forms the internal data state of the functor,
that's all.

Cheers
Paavo

carlgl...@gmail.com

unread,
Jan 18, 2019, 8:54:55 AM1/18/19
to
IMHO, I think it is important to have a published official grammar. I bought a copy of the "Special 3rd edition" of "The C++ Programming Language," because it contains "Appendix A The C++ Grammar."

The 4th Edition of "The C++ Programming Language" does not appear to have an "Appendix A" nor any C++ grammar summary anywhere in the book.

I was hoping to find a copy of the missing "Appendix A The C++ grammar" (summary) on the website of Bjarne Stroustrup, one that corresponds to the 4th Edition but am unable to locate any such grammar.

Perhaps someone here knows where to find such a grammar summary that goes with the 4th Edition?

Carl
---

On Saturday, January 5, 2019 at 2:32:10 PM UTC-8, Unto Sten wrote:
> Hello!
>
> Years ago, I read Bjärne's C++ Book, third edition.
> I never did any real C++ programming, but I have
> read some C++ source, and am familiar with most of the
> core concepts.
>
> Since then, C++ Book 4th edition has appeared,
> covering C++11.
>
> But after that we have C++14 and Bjärne says in:
>
> http://www.stroustrup.com/C++11FAQ.html
>
> ####################################################
> Will there be a C++1y?
>
> Almost certainly -- and not just because the committee
> has slipped the deadline for C++0x. The plans for minor
> revisions, C++14, are well advanced (the features have
> been voted into the working draft and implemented), and
> the plan is for a major revision in 2017, C++17.
> ####################################################
>
> Bjärne says C++11 feels "like a new language" and
> yet C++17 is planned to be "a major revision"...?
>
> Yes, C++17 materialized:
>
> https://en.wikipedia.org/wiki/C%2B%2B17
>
> According to Wikipedia, the next standard is going to
> be C++20.
>
> I do not mean to be trolling, but I would like
> to take the time learn C++ properly, since
> incorrect C++ usage seems to me as dangerous as
> incorrect usage of C. So many chances to create
> nasty bugs when falling victim to undefined behaviour
> and so on.
>
> But this C++ language seems to grow and grow, and
> new standards arrive frequently. I am pretty confused.
>
> What on earth should one do to learn this language?
> Yeah, they say most C++ projects use only a subset
> of C++ features, but those subsets can be different for
> different groups - forcing you to learn a lot.
>
> Quite frankly, I am going crazy here, not knowing
> how to start. Yeah, I know OOP pretty well, but I
> am thinking of all those C++ details that you have
> to master...
>
> I guess I should buy C++ Book 4th edition, but I
> am afraid those new standards make some features
> outdated, and introduce new ways of doing things.
>
> Best regards,
> Unto Sten

Manfred

unread,
Jan 18, 2019, 9:57:09 AM1/18/19
to
On 1/18/2019 2:54 PM, carlgl...@gmail.com wrote:
> IMHO, I think it is important to have a published official grammar. I bought a copy of the "Special 3rd edition" of "The C++ Programming Language," because it contains "Appendix A The C++ Grammar."
>
> The 4th Edition of "The C++ Programming Language" does not appear to have an "Appendix A" nor any C++ grammar summary anywhere in the book.
>
> I was hoping to find a copy of the missing "Appendix A The C++ grammar" (summary) on the website of Bjarne Stroustrup, one that corresponds to the 4th Edition but am unable to locate any such grammar.
>
> Perhaps someone here knows where to find such a grammar summary that goes with the 4th Edition?
>
> Carl

You can always refer to the standard:
The ISO committee publishes the final drafts for all revisions:
http://www.open-std.org/JTC1/SC22/WG21/docs/standards

It is hard to read, but as a strict grammar reference it can work as an
addendum to Bjarne's book, i.e. the book gives the readable rationale,
and the standard provides the strict details.

More accessible is:
https://en.cppreference.com/w/

carlgl...@gmail.com

unread,
Jan 18, 2019, 5:04:29 PM1/18/19
to
So I need C++11 grammar to use with TC++PL4, which is N3242 draft standard?
Carl
---
On Friday, January 18, 2019 at 6:57:09 AM UTC-8, Manfred wrote:

james...@alumni.caltech.edu

unread,
Jan 18, 2019, 5:19:24 PM1/18/19
to
On Friday, January 18, 2019 at 5:04:29 PM UTC-5, carlgl...@gmail.com wrote:
> So I need C++11 grammar to use with TC++PL4, which is N3242 draft standard?

Yes. Amazon.com's description for the 4th edition indicates that it
describes C++2011, so if you desire an exact match, you shouldn't use a
more current version of that standard. Personally, though, I think
you'll be better off with the latest draft version: n4659.pdf, even if
it doesn't exactly match the book.

Tim Rentsch

unread,
Mar 1, 2019, 8:59:51 AM3/1/19
to
Manfred <non...@add.invalid> writes:

> On 1/7/2019 11:44 PM, Jorgen Grahn wrote:
>
>> On Sat, 2019-01-05, Unto Sten wrote:
[...]

>>> But this C++ language seems to grow and grow, and
>>> new standards arrive frequently. I am pretty confused.
>
> I a no fan of ever-changing languages either. In fact I consider
> every change to the language a cost, which is not always paid off
> by the benefit of the new feature.
> More precisely, I do not agree with those who claim that a new
> feature is inexpensive if you don't want to use it - even if you
> don't use it, you need to know about it, and this is not
> cost-free.

Completely agree.
0 new messages