The C++ Iceberg
An “iceberg” chart of C++ quirks prompts a wide-ranging look at the language’s sharp edges and evolving design. Commenters dissect features like `constexpr`, `inline`, `shared_ptr`, and `std::unreachable`, but keep returning to undefined behavior—how compilers exploit it for optimization, why it leads to surprising and sometimes dangerous outcomes, and how tools like sanitizers or alternative languages (e.g., Rust, D) try to mitigate these risks. Overall, the exchange highlights the trade‑off between C++’s performance and flexibility and the complexity and fragility this imposes on developers.
constexpr, consteval, and D’s CTFE
- Discussion on how
constexpris more than a “hint”:constexprvariables must be compile-time;constexprfunctions may run at compile time or runtime.constevalfunctions must be evaluated at compile time.
- Comparisons to D: any function can be evaluated at compile time when used in a constant expression; if it can’t, it’s a compile error. No need for tagging.
Meaning of inline
- Debate over what
inlinereally means:- One side: it primarily affects linkage and allows multiple definitions (often implemented via weak symbols).
- Others initially associate it with an inlining optimization hint or “definition provided inline with declaration,” which is challenged as imprecise.
- Clarification that member and template functions are implicitly inline; location (header vs source) and textual placement are separate issues.
Undefined Behavior (UB) as a Core Concern
- Many comments call UB “terrifying,” especially for memory safety (bad pointers, out-of-bounds, use-after-free, data races).
- Some argue UB existed from the start and compiler optimizations now make its effects less predictable; others say compilers choose to aggressively exploit UB.
- Debate on whether all UB could be checked at runtime; some say many checks would be too expensive or get optimized away.
- Examples: invalid pointer dereference, array index out of range, lifetime violations, integer overflow, division by zero,
gets()-style APIs. - Disagreement over whether UB is “just” programmer responsibility vs a serious modern design flaw.
std::unreachable and Adding New UB
- Confusion and criticism: why add a facility that is pure UB instead of a guaranteed trap/abort?
- Supporters say it is an important optimization hint, similar to Rust’s unsafe “unreachable,” and implementations can still trap in debug builds.
- Some emphasize it allows customizable behavior via build flags; others counter that this should be implementation-defined, not undefined.
Tools and Mitigations
- Multiple mentions of sanitizers (ASAN, UBSAN, TSAN), compiler warnings, static analyzers, fuzzing, and modern IDE tooling as essential to tame UB, while noting they still depend on triggering the bad paths.
Other C++ Iceberg Notes
- Comments praise the interactive iceberg and the linked explanations behind each item.
- Brief debates:
std::shared_ptrvsshared_ptr<const T>and copy-on-write for better value semantics.std::optionalbeing a monad is viewed as positive and useful.- Light humor around “Godbolt” being both a tool nickname and a real person.