TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A C++ Mixin System

74 pointsby luu6 months ago

15 comments

Svetlitski6 months ago
&gt; 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<p>An interesting option in this space is rpp [1], which bills itself as a “Minimal Rust-inspired C++20 STL replacement”<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;TheNumbat&#x2F;rpp">https:&#x2F;&#x2F;github.com&#x2F;TheNumbat&#x2F;rpp</a>
评论 #42219844 未加载
kazinator6 months ago
GNU C++ once had a system called Signatures, could support mixing in. It was removed. Many years ago now I think.<p>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.
评论 #42219221 未加载
cisters6 months ago
In a language with ad-hoc polymorphism like C++, mixins seems entirely unnecessary.<p>You can just declare by convention that a freestanding clone(T x) -&gt; T function should exist for it to be &#x27;cloneable&#x27;.
评论 #42215174 未加载
评论 #42214400 未加载
mightyham6 months ago
While I&#x27;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&lt;&gt;())` so that the map is thread-safe and allows mixin instances to be garbage collected.
Matheus286 months ago
<p><pre><code> std::optional&lt;T&amp;&gt; </code></pre> Can&#x27;t have optional references in C++. Use either std::reference_wrapper, or just a pointer
评论 #42216783 未加载
heresie-dabord6 months ago
It seems messy and even the author of TFA is unconvinced.<p>How does a mixin compare to <i>role</i> or <i>interface</i> in languages that do not have multiple inheritance?
cherryteastain6 months ago
I don&#x27;t really see the point when C++ already lets you write<p>void foo(auto&amp; t) { t.bar(); }<p>which can be called with any class that has the .bar() method anyway.
评论 #42214259 未加载
评论 #42220294 未加载
tempodox6 months ago
That `String` leaks memory, it doesn&#x27;t have a destructor.
评论 #42217875 未加载
评论 #42215378 未加载
Asooka6 months ago
Code with types on the right like this makes me very sad<p><pre><code> static auto create(const char* data) -&gt; Result&lt;String&gt; </code></pre> 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:<p><pre><code> static auto create(const char* data) -&gt; Result&lt;String&gt; </code></pre> Future readers will thank you.
评论 #42214327 未加载
评论 #42215844 未加载
评论 #42218065 未加载
评论 #42215616 未加载
bnastic6 months ago
A lot of this stuff has been investigated in Mr Alexandrescu&#x27;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.
surajrmal6 months ago
We&#x27;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.
binary1326 months ago
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.
DidYaWipe6 months ago
&quot;Mixin?&quot; What is that supposed to mean?
DidYaWipe6 months ago
&quot;Mixin?&quot; What&#x27;s it mixin&#x27;?
评论 #42219839 未加载
bfrog6 months ago
C++ is somehow aesthetically dis pleasing, thus mixin idea doesn’t change the needle for me.