November 19th, 2024

A C++ Mixin System

A proposed C++ mixin system aims to improve code organization by using Rust-like error handling, data constructors, and CRTP, though the author is uncertain about its practical implementation.

Read original articleLink Icon
CuriositySkepticismConfusion
A C++ Mixin System

The article discusses a proposed C++ mixin system that aims to enhance code organization and logic through the use of mixins, drawing inspiration from Rust traits and C++ concepts. The author envisions a framework that diverges from traditional C++ by adopting a Rust-like standard library approach, where errors are managed through Result<> or Option<> classes instead of exceptions. Key features of this system include the use of data constructors for object initialization, factory functions for non-trivial object creation, and a default constructor that initializes objects to a no-operation (NOP) state. The author also emphasizes the importance of assertion errors for invalid interactions with NOP objects. The mixin concept is illustrated through a Clone trait example, which includes a clone() method and compile-time assertions for interface implementation. The article further explores the Curiously Recurring Template Pattern (CRTP) to avoid virtual table overhead while allowing for polymorphic behavior. The author acknowledges that while some ideas may already exist in the C++ standard library, the proposed system could serve as a demonstration of CRTP's capabilities and provide utility for earlier C++ versions. Ultimately, the author expresses uncertainty about the practicality of implementing this system but finds the concept intriguing.

- The proposed C++ mixin system aims to enhance code organization and logic.

- It draws inspiration from Rust traits and emphasizes error handling through Result<> or Option<> classes.

- Key features include data constructors, factory functions, and a NOP state for objects.

- The Curiously Recurring Template Pattern (CRTP) is utilized to minimize virtual table overhead.

- The author finds the concept interesting but is uncertain about its practical implementation.

AI: What people are saying
The comments on the proposed C++ mixin system reveal a mix of skepticism and curiosity about its practicality and relevance in C++.
  • Several commenters question the necessity of mixins in C++, given the language's existing features like ad-hoc polymorphism and function templates.
  • There are references to past systems and libraries, such as GNU C++ Signatures and Alexandrescu's work, indicating that similar concepts have been explored before.
  • Some users express confusion about the article's content and the proposed design, suggesting it may be overly complex or not intuitive.
  • Concerns about C++'s aesthetic and ergonomic issues are raised, with suggestions for improving code readability.
  • Overall, there is a general sense of uncertainty regarding the mixin system's implementation and its potential benefits.
Link Icon 15 comments
By @Svetlitski - 6 months
> I think the big asterick to all of this design is that my ideal framework would not look like standard C++ but like a slightly weirder Rust stdlib

An interesting option in this space is rpp [1], which bills itself as a “Minimal Rust-inspired C++20 STL replacement”

[1]: https://github.com/TheNumbat/rpp

By @kazinator - 6 months
GNU C++ once had a system called Signatures, could support mixing in. It was removed. Many years ago now I think.

A signature resembles a class declaration in that it specifies member functions. A signature is never instantiated. Rather any class which has those member functions conforms to the signature, and a signature-typed pointer can point to instances of that class. Thus signatures bring about quack-like-a-duck polymorphism not requiring inheritance.

By @cisters - 6 months
In a language with ad-hoc polymorphism like C++, mixins seems entirely unnecessary.

You can just declare by convention that a freestanding clone(T x) -> T function should exist for it to be 'cloneable'.

By @mightyham - 6 months
While I've never really found much practical use for mixins, it is fairly easy to create a runtime system for them in Java. Any interface can become a mixin simply by storing state in a static global hashmap with `this` as the key to the map. Specifically for the map, I would use `Collections.synchronizedMap(new WeakHashMap<>())` so that the map is thread-safe and allows mixin instances to be garbage collected.
By @Matheus28 - 6 months

    std::optional<T&>
Can't have optional references in C++. Use either std::reference_wrapper, or just a pointer
By @heresie-dabord - 6 months
It seems messy and even the author of TFA is unconvinced.

How does a mixin compare to role or interface in languages that do not have multiple inheritance?

By @cherryteastain - 6 months
I don't really see the point when C++ already lets you write

void foo(auto& t) { t.bar(); }

which can be called with any class that has the .bar() method anyway.

By @tempodox - 6 months
That `String` leaks memory, it doesn't have a destructor.
By @Asooka - 6 months
Code with types on the right like this makes me very sad

    static
    auto create(const char* data) -> Result<String>
Types are a lot more ergonomic on the left - the return type of a function and the type of a variable are very important for understanding and skimming code. When the return type is on the right, it is physically very far from the name of the object and I have to scan the entire line with my eyes to get the same amount of information I would get by just looking at the left column and scrolling down if it were on the left. I am pretty sure in another 20 years types on the right will be regarded as one of the ergonomic fails of current language design. At least, if have to do this, put the type right under the object name, like so:

    static auto
    create(const char* data)
    -> Result<String>
Future readers will thank you.
By @bnastic - 6 months
A lot of this stuff has been investigated in Mr Alexandrescu's ironically named book Modern C++. Typelists (before variadic templates) recursive templates and componenet-like assembling of classes, etc. I imagine there is a modern-modern-c++ version of Loki library somewhere on github.
By @surajrmal - 6 months
We've used this pattern for years. It definitely delivers in terms of being lower overhead. I will say that compiler errors can be nonsense though.
By @binary132 - 6 months
I am curious about this idea, and maybe it’s a “me problem”, but I’m having a very hard time following the article. There’s a lot going on here.
By @DidYaWipe - 6 months
"Mixin?" What is that supposed to mean?
By @DidYaWipe - 6 months
"Mixin?" What's it mixin'?
By @bfrog - 6 months
C++ is somehow aesthetically dis pleasing, thus mixin idea doesn’t change the needle for me.