56
votes
Accepted
Why are volatile objects so difficult to work with in C++?
What volatile actually means
The meaning of volatile in C and C++ is often poorly understood. The main use case for ...
33
votes
Accepted
Why would a language have a concept of undefined behavior instead of raising an error?
This is a controversial topic
The C89 standard, section 3.16, defines:
undefined behavior: Behavior, upon use of a nonportable or erroneous
program construct, or of erroneous data, or of ...
26
votes
Accepted
How is clang able to evaluate loops without getting stuck in an infinite loop?
Because this is an optimization Clang does, you can use godbolt.org to explore it optimization pass by optimization pass. I've turned on the "Optimization Pipeline Viewer", set the "...
25
votes
What obstacles prevented C and C++ from standardizing π?
There is no deep reason. The answer is social:
The C standard does not specify any constant for π. We can only speculate on why, but ultimately, the set of things the C standard does and does not ...
22
votes
Accepted
Why would a language need to have trap representations?
Trap representations are usually a property of the underlying hardware. If they exist, a programming language has to choose a behaviour:
Ensure that every value is initialised to a non-trap value ...
21
votes
When would a Java style enum be better than a C++ style enum, and vice versa?
For a complete comparison, we should add a 3rd (and 4th?) type of enum: Sum Types, as in Haskell or Rust.
Scope
C and C++ plain ...
21
votes
Accepted
Why did C++ standard library name the containers map and unordered_map instead of map and ordered_map?
The short answer: Historical reasons.
Alexander Stepanov's philosophy was always that the algorithms come first. One analogy he always liked was with mathematics. When mathematicians present their ...
20
votes
Accepted
What is the explicit list of the situations that require RAII?
There is no complete list, because many of the most "interesting" examples are application-specific.
Consider something like transaction safety in a database server: a transaction must ...
19
votes
Why would a language have a concept of undefined behavior instead of raising an error?
Performance
Checking for errors at runtime is quite expensive. Branching operators massively are slow instructions, especially since they prevent many optimizations. If every basic operator needed a ...
16
votes
What is the explicit list of the situations that require RAII?
You should basically never write code that requires you to write corresponding code later in the control flow.
The obvious examples are resource acquisition things like allocating memory (you have to ...
15
votes
How is clang able to evaluate loops without getting stuck in an infinite loop?
There are basically two reasons for this: the halting problem is semidecidable (i.e., we can solve it, sometimes), and the C++ standard allows LLVM to cheat and pretend it solved the halting problem ...
14
votes
Why would a language have a concept of undefined behavior instead of raising an error?
Besides performance and ease of writing an implementation, there is one other reason for undefined behavior.
If some property is impossible to check for and cannot be defined because it's ...
14
votes
Why might a language avoid reallocation?
There is potentially unknown number of variables pointing to ("referencing") the same Java array. It cannot be "reallocated" because these references may end up pointing to nothing ...
14
votes
What is the explicit list of the situations that require RAII?
There are no things that require RAII to exist, (as evidenced by various languages not having it). RAII helps in avoiding resource management bugs but is not required to solve the resource management ...
13
votes
Why would a language need to have trap representations?
One example is that some computers would trap if an invalid bit pattern were loaded into one of the pointer registers (such as the segment selector of an 80286 protected-mode far pointer, necessary on ...
11
votes
Accepted
How are C++ style references implemented behind the scenes? Could they be implemented without pointers?
From an implementation perspective, there is one very important difference between a pointer and a reference in C++: references cannot be null.
This is undefined behaviour:
...
10
votes
Accepted
What obstacles prevented C and C++ from standardizing π?
Both languages have an upper bound to their precision of their floating point types (float double ...
10
votes
Why does C++ allow extra arguments to printf?
printf
With respect to printf, the C++ committee didn't really have a lot of choice. printf was "inherited" from C, ...
10
votes
How is clang able to evaluate loops without getting stuck in an infinite loop?
Clang neither solves the halting problem, nor it is evaluating the loop, potentially forever. It is only optimizing the loop once, as part of the compilation process.
Let's compile the code as an ...
9
votes
Why would a language have a concept of undefined behavior instead of raising an error?
The premise of undefined behavior is assuming that erroneous conditions never happen. The responsibility of checking and avoiding such conditions is placed on the programmer and not on the ...
9
votes
Why would a language have a concept of undefined behavior instead of raising an error?
The point about UB is that the language – the compiler, or the interpreter – does not have to worry about extreme or edge cases. It's the programmer that has to worry about such situations. In other ...
9
votes
Accepted
Why does C++ require private methods to be declared with public ones?
As also explained by the other answer, all member variables need to be declared in the header, because by themself all types in C++ are value types. This implies that the compiler needs to know their ...
8
votes
Why would a language have a concept of undefined behavior instead of raising an error?
From the ISO C++ wiki's FAQ item, "Why are some things left undefined in C++?":
Because machines differ and because C left many things undefined. For details, including definitions of the ...
8
votes
What obstacles prevented C and C++ from standardizing π?
I can't find anything in the Rationale that specifically addresses mathematical constants, or the lack thereof, so I can only speculate.
Other popular programming languages at the time didn't have a ...
8
votes
Why might a language avoid reallocation?
Java couldn't allow this because of how its memory is laid out. There are four pools, each of increasing size. Every time an object is created, it is added to the smallest pool, in a contiguous manner....
8
votes
Accepted
Why don't languages do automatic pimpl?
TL;DR: Fast compilation or fast runtime, pick one.
PIMPL is not the answer
A data-member being private only prevents naming the data-member in the source code. It does not preclude the compiler from ...
7
votes
When would a Java style enum be better than a C++ style enum, and vice versa?
In C++ you can have a plain enum or an enum class, and the enum class is similar in some way to Java enums. So plain enum are more likely the one you are describing here when you say C++ enum.
In that ...
7
votes
Why did std::set not have a contains function until C++20?
Because they wanted all containers to adhere to the same interface as much as possible, and .count() made sense for more containers, and still worked equivalently ...
7
votes
How is clang able to evaluate loops without getting stuck in an infinite loop?
There is a lot of talk about inductive types and inlining here, but the optimisations in the code can be achieved through far simpler methods.
Let's look at the function
...
6
votes
Why would a language need to have trap representations?
One example of a where an implementation could use this is signalling NaN.
Some implementations have a concept of 'signalling NaN' with unconventional behavior, such as raising a signal upon being ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
c++ × 25c × 5
language-design × 5
standard-library × 5
history × 4
java × 2
implementation × 2
math × 2
undefined-behavior × 2
numbers × 1
error-handling × 1
rust × 1
arrays × 1
optimization × 1
functions × 1
loops × 1
object-oriented × 1
compilers × 1
memory-safety × 1
memory-management × 1
namespaces × 1
exceptions × 1
static-analysis × 1
iterators × 1
declarations × 1