logoalt Hacker News

simonask • today at 6:20 PM • 1 reply • view on HN

Exactly. C++ programmer confidence is a bell curve, where the X axis is skill level.

The more you know about programming in C++, the less confident you become that your code is actually correct, because you've seen before how the most seemingly innocuous decisions turned out to be massive UB footguns. Even worse if you know anything about how computers actually work - that's not relevant, C++ like C targets an abstract virtual machine with semantics unlike any computer ever built.

The simplest example is signed integer overflow: the bright young student knows all about Two's Complement and mistakenly believes all is well, and the wizened but weary mentor must waste everyone's time to avoid summoning nasal demons.

What an unbelievable waste.


Replies

sedictious • today at 8:35 PM

I'd say C++ is so vast that you can keep on encountering bell curves like that. I start working on a project, building confidence in the subset of C++ I'm working with and when I start feeling good about myself, I'm hit with a completely foreign concept

cppquiz is a nice test beyond "trivia". There are a few hard questions addressing topics I've never encountered. For example:

```

#include <iostream>

volatile int a;

int main() { std::cout << (a + a); }

```

I would assume that here a is initialised to 0 since it has static lifetime so I'd expect this to just print 0. But the C++ standard specifies that:

> Reading an object designated by a volatile glvalue ([basic.lval]), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression (or a subexpression) in general includes both value computations (including determining the identity of an object for glvalue evaluation and fetching a value previously assigned to an object for prvalue evaluation) and initiation of side effects. When a call to a library I/O function returns or an access through a volatile glvalue is evaluated the side effect is considered complete, even though some external actions implied by the call (such as the I/O itself) or by the volatile access may not have completed yet.

and if you have side effects unsequenced relative to other side effects (as is the case with operations of the same operand), this is considered UB