C++ left arrow operator (2016)

Playful and “Evil” C++ Idioms

  • Thread reacts to a custom left-arrow operator as amusing, clever, and slightly horrifying.
  • Other “evil” patterns are mentioned: calling delete this, manually calling the destructor then placement-new on this, and intrusive reference counting.
  • Some argue delete this is acceptable for intrusive ref-counted objects or COM-style Release, others prefer smart pointers to hide such patterns.
  • Affine/move-type languages (e.g., Rust) are cited as making “destroying this” conceptually safer than manual memory delete.

Operator Overloading: Power vs Abuse

  • Many see operator overloading as fine when used with taste; abuses are called out as exceptions, not the rule.
  • Examples of controversial or playful uses:
    • <</>> for streams, network messages, and container insertion.
    • + for composing GUI widgets.
    • * for RAII guards.
    • / for filesystem paths.
    • Exotic constructs like “analog literals” and arrow-like or tadpole operators.
  • Some praise libraries (e.g., for linear algebra) where operators dramatically improve readability.
  • Others argue operators in general were a design mistake: too many possible semantics (overflow behavior, etc.) and poor readability compared to named functions.

Iostreams and Formatting

  • The use of << for I/O is criticized as a poor design that didn’t spread beyond the standard library.
  • Strong preference from some for format-string APIs (std::format/std::print, printf-style, or fmt library) due to usability and type safety.
  • Counterpoint: iostream problems are mostly unrelated to operator overloading itself.

Functors, Lambdas, and Operator()

  • Overloaded operator() historically enabled functors/closures; lambdas now cover most use cases.
  • Still, custom functor types are seen as necessary for some patterns (e.g., std::function internals, multi-overload visitors, destructors with behavior).

Pointers-to-Members and Delegates

  • C++ pointers-to-members are described as confusing and implementation-defined.
  • One view initially (later softened) equates them to a “pair” of instance pointer and function; others insist they contain no instance state and only encode offsets/dispatch data.
  • Comparison with “delegate” or “fat pointer” mechanisms in other languages (D, C#, Virgil, Delphi, Python):
    • Fat pointers are praised for simplicity and first-class closures.
    • C++’s leaner model is defended as more flexible and cheaper, aligning with “don’t pay for what you don’t use,” with std::function as a heavier abstraction when needed.
  • Debate arises over performance trade-offs, virtual dispatch, inlining, and compilation models.

General Reflections on C++ and Operators

  • Some commenters express affection for C++’s power and creativity; others call it overly complex, even “comedy” or “abominable.”
  • There is recurring tension between clever expressiveness (custom operators, tricks) and long-term code readability and maintainability.