Skip to main content
55 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
  • 8,721
31 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
  • 4,726
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
  • 8,721
21 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,038
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's user avatar
  • 8,349
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
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,062
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
  • 4,726
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
  • 19.7k
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
  • 8,321
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
  • 778
9 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
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

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
  • 261
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,959
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
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
  • 923
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
  • 8,321
6 votes

What can be learned from problems (and associated solutions / goals to strive for) C++ committee members have identified with their proposal culture?

Here's what Bjarne Stroustrup said on this topic during the 2017 CppCon "Grill the Committee" panel (at t=14:23). TL;DR: Bjarne observes that there's a lack of collaboration, a not very ...
starball's user avatar
  • 1,652
6 votes

When would a Java style enum be better than a C++ style enum, and vice versa?

C++ enums are essentially just int constants. This makes them efficient to implement, and usable for low-level or performance-critical programs. OTOH, Java-style ...
dan04's user avatar
  • 1,959
5 votes

Why does C++ require private methods to be declared with public ones?

The private attributes may effect the position of public attributes. If you have a class like this: class A { public: int a; } Here, ...
mousetail's user avatar
  • 8,349
5 votes
Accepted

How does the C++ Committee generally decide what does or doesn't go in its standard library?

Bryce Adelstein Lelbach spoke on this in the CppCon 2020 Standards Committee Fireside Chat at t=17:16 (Bryce is the chair of the Standard C++ Library Evolution group at the time of this writing and ...
starball's user avatar
  • 1,652
5 votes

Why might a language avoid reallocation?

In C, the memory location of the object affected by calling realloc may change, meaning you have to update your pointers to the object after calling it. The ...
occipita's user avatar
  • 569
5 votes

Why are volatile objects so difficult to work with in C++?

In a follow on comment, OP mentioned that his use case is on ISRs. Because an ISR is so tightly constrained on what it can do, and the nature of interrupts guarantees their memory ordering, the ...
DanielN's user avatar
  • 129
5 votes

Why does C++ allow extra arguments to printf?

The mechanism for processing variadic argument lists in C++ was inherited from C. This mechanism does not provide a way to determine the number of arguments that were supplied, it simply supports ...
Barmar's user avatar
  • 1,901
5 votes
Accepted

Why does C++'s unordered_map::erase fully invalidate iterators, not even supporting advancing?

Check the return value of std::unordered_map::erase. It returns an iterator. In fact it is the iterator pointing to the element after the removed element. what you ...
ratchet freak's user avatar
5 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,038
4 votes

How are C++ style references implemented behind the scenes? Could they be implemented without pointers?

I believe such optimizations are carried out at a later stage of compilation. The compiler back-end, when analyzing the function's IR, is able to notice that a reference is only used within the ...
abel1502's user avatar
  • 2,527

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