Skip to main content
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 ...
Alexis King's user avatar
  • 10.4k
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 ...
Pseudonym's user avatar
  • 5,376
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 "...
Simon Farnsworth's user avatar
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 ...
Alexis King's user avatar
  • 10.4k
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 ...
Toby Speight's user avatar
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 ...
Matthieu M.'s user avatar
  • 3,549
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 ...
Pseudonym's user avatar
  • 5,376
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 ...
Pseudonym's user avatar
  • 5,376
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 ...
mousetail 'he-him''s user avatar
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 ...
Matt Timmermans's user avatar
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 ...
Moonchild's user avatar
  • 1,086
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 ...
Gavin D. Howard's user avatar
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 ...
Audrius Meškauskas's user avatar
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 ...
ratchet freak's user avatar
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 ...
Davislor's user avatar
  • 1,190
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: ...
Pseudonym's user avatar
  • 5,376
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 ...
kaya3's user avatar
  • 20.8k
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, ...
Jerry Coffin's user avatar
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 ...
André L F S Bacci's user avatar
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 ...
CPlus's user avatar
  • 9,153
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 ...
printf's user avatar
  • 271
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 ...
chrysante's user avatar
  • 788
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 ...
starball's user avatar
  • 1,652
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 ...
dan04's user avatar
  • 1,979
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....
phyrfox's user avatar
  • 221
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 ...
Matthieu M.'s user avatar
  • 3,549
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 ...
coredump's user avatar
  • 943
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 ...
Nick Matteo's user avatar
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 ...
John's user avatar
  • 101
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 ...
CPlus's user avatar
  • 9,153

Only top scored, non community-wiki answers of a minimum length are eligible