Remix.run Logo
▲ sedictious 2 hours ago

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

▲0x457 an hour ago | parent [-]

In practice, it will be 0, assuming no external modification of a. Strictly under standard, its UB.

▲sedictious 41 minutes ago | parent [-]

yes which is exactly why you don't keep those things in mind most of the times. They are mostly academic exercises yet they pop up in your actual code from time to time and you find yourself furiously scratching your head. But my point is that C++ is huge and there is no way any single human can keep the whole standard in their head.