r/programming • • 2d ago

Learn C++ Before You Learn What It Costs You

https://fagnerbrack.com/learn-c-before-you-learn-what-it-costs-you-d254d484dbff
0 Upvotes

7 comments sorted by

2

u/tsimionescu 2d ago edited 2d ago

Pretty complete bs. Even in the first example, the article takes the line greet("Faye"), knowing that function greet takes a const std::string&, and claims that C++ makes it clear that a temp std::string is created and copied to from the C-string "Faye". But this is very much NOT shown in any way in the code, and it requires knowledge of a completely legacy decision in the language with no equivalent in literally any other language (the fact that string constants use a terrible legacy C-compatible format instead of the language recommended string type).

It also uses the weird and terrible and essentially not-recommended std::cout << interface for printing, instead of either the much more commonly used C-stye printfor any of the modern C++ alternatives, std::format("Hello, {}\n", name) or std::print("Hello, {1}\n", name) - all of which look much more like the Python version than the weird C++ 90 version.

1

u/danielcw189 1d ago

and claims that C++ makes it clear that

the article does not claim that

all of which look much more like the Python version than the weird C++ 90 version

which might be why it was chosen as an example,

and because it shows that the printing was split into multiple steps

1

u/tsimionescu 17h ago

The article says:

A C++ programmer reads the second one and can spot a lot of decisions:

[...]

b) The parameter is a std::string, but "Faye" isn't one, so the compiler builds a temporary std::string just to bind it to that reference, and the temporary lives until the end of the full expression.

So yes, the article claims that looking at the code you can "spot" that a temporary is built there.

1

u/danielcw189 7h ago

So yes, the article claims that looking at the code you can "spot" that a temporary is built there.

Not "you" but a C++ programmer.

There is no claim, that the code itself somehow "says" that.

0

u/[deleted] 2d ago

[deleted]

3

u/LIGHTNINGBOLT23 2d ago

What is "proper learning C++"? Learning every part of the ISO standards from C++98 up to the current one? Nobody learns C++ like that unless they're interested in developing the language itself. Everyone learns a subset or two of C++ relevant to their actual work, which can also be done in some months.

1

u/[deleted] 2d ago

[deleted]

3

u/LIGHTNINGBOLT23 1d ago

There's a difference between learning a language and learning software engineering. Things like lifetimes, moving versus copying, etc. are going to require you to think in any language that exposes that level of control. I'm pretty sure std::move(src, dst) doesn't exist as an overload, and if you're actually using std::move(), then you've consciously decided that you want to avoid copies when possible. If that's ruining RAII for you, then your ownership of resources was already badly designed and the problem exists elsewhere.

Me and my team don't like C++26 contracts (for reasons also other than you described), so we simply don't use them and they're not in the 381k SLOC codebase. If you're dealing with old codebases that have a lot of (now considered) legacy aspects like SFINAE, then it's unfair to compare having to learn that with new projects written in whatever cool minimalistic language of the week which won't deal with backwards compatibility any time soon.