It looks like it to me. I think you could make some pretty readable and high performance variants and tuples with c++26 alone. c++29 if some of the work aimed at extending reflections code generation stuff gets in will enable so much more.
There's not much point benchmarking an experimental compiler, but I've seen a roughly 20x speedup compared to libc++'s variant with this. That's quite significant.
Yep, but I wouldn't count on it being standardised in C++26. It may be, but there are a few people that aren't too keen on it and it may well get postponed to a later release. See this proposal for instance
That paper did not gain consensus though, and define_aggregate/etc are in the C++26 draft that is currently being vetted. So unless new information is found that would warrant a removal it will be in.
Forgive me, because I am still a novice to reflection syntax, but surely Members should be a union here not a struct? Our QuantifiedType can presumably only hold a single possible type, which means we want the storage to overlap when possible no?
I groked it shortly after posting the comment. I have a feeling I'm going to be posting a lot of dumb questions for awhile until I sit down and bang my head against the spec for a while
All told, a monad in X is just a monoid in the category of endofunctors of X, with product Γ replaced by composition of endofunctors and unit set by the identity endofunctor.
There is such a weird difference in embedded. We are for example c++23 in our embedded because we recompile the world when making a release anyway. We have to recertify anyway at the same cost and we get to update our stuff. So aside from bootloaders which can drag behind a bit we are usually able to move up our standards. But I know others are stuck with proprietary compilers, external libraries that are not source and so on. And they only get to work with never stuff if they are lucky.
Not that c++23 buys us much as yet because no compilers we use has implemented std::start_lifetime_as yet, but at least we are getting ready to change all of our stuff into modules within a year or two (since we have source code for everything that is an option we have).
Yeah embedded is wild. On some chips you get bad toolchains like some old gcc 4.x with just enough C/C++to get by, or if you're very unlucky Green Hills or some other crap. Then there's esp32 that's been supporting basically full C++ (with exceptions and rtti!) and Rust for years
Luckily most embedded devs work with ARM and we are getting all the features in there. AVR also has a fully up to date GCC compiler as well. Maybe they use PIC24 or some of the other 16 processors. So along with RISC-V and xtensa (esp32) most of those somewhat modern and popular chips have near full support.
I am hoping for a design using a RISC-V. Funny you should comment because we are also thanks to your talks looking into getting exceptions into our stack too because we like your students really do not like the expected pattern or the error code having used it everywhere for 3-4 years now (since we saw a talk on Expected and replicated for our purposes).
That's amazing! Love to hear it π. One word of caution when it comes to RISC-V, I believe they only support the DWARF unwind instructions. Those instructions are less compact than what could be for RISC-V. Regardless, RISC-v is on my list of devices to support with my exception runtime. My next CppCon will be about my journey improving exception performance by 10x (so far π). So hopefully in the future, the benefits that I claim for exceptions aren't just relevant to arm and x86 with compact unwind instructions.
Fun fact, besides std::current_exception, which requires TLS, exceptions should work so long as you put noexcept on your thread functions (or wrap them), that way the unwinder knows to stop unwinding at the thread boundary. When I implement <thread> (with stack size hint) for FreeRTOS, it'll take care of all of the noexcept and TLS stuff for you so you get access to the current exception. And for those that don't want to use std::thread with a stack hint and name, I can do a write up for enabling TLS for exceptions for FreeRTOS. And if you use a different RTOS, then we can look into supporting them as well. So stay tuned. π
I am using FreeRTOS and we are not using std::thread we have our own abstraction around FreeRTOS so a simple description or write up would be super helpful. And I will stay tuned -- do not worry about that -- because it will fall to me to implement it later on and the developers I support are really tired of writing:
if (auto result = some_object.some_call_that_returns_expected(); !result) {
struct C {
template<typename T>
C(T t) : t_(t) {}
T t_;
};
That's roughly the idea of existential types. Simplifying, OP makes member an std::any to make the member concrete and all the other machinery exists for the sake of automating any_casts.
Now that I think about it, it looks a bit like Rust trait.
That CanFAndG is like a trait. However, neither A or B βimplementβ the trait (explicitly state it), the just conform to it. + dynamic dispatch built in I guess.
But it would still be a closed set, right? In the sense that to add new items you'll have to recompile? Inheritance, for example, does not have this issue.
I'm kinda noodling on a "Reflecting Concepts" idea/proposal to remove the need to create the CanFAndG struct, and instead using concepts to indicate that functions exist on a type and generate a vtable for just those functions. It's cool you got this working without that.
The ability to use concepts as template parameters in cpp26 will make this much easier.
I think it's possible to make some decisions based on what you have available to you, but there would definitely a subset of features you could use with concepts you can use for something like this.
Since there are only three left among those that are still being updated, or forks thereof, C++ and upstream clang latest, alongside MSBuild, CMake/ninja, build2 or xmake.
GCC is getting there.
All the downstream from clang and GCC, depends on when they bother to update.
Everything else is mostly on C++17, and probably won't be getting any updates.
I guess the reason is simple. It's becoming increasingly complex to implement the latest C++ features, and no one wants to pay for the basic C++ compiler anymore. Tooling and compilers for other languages are also free.
Now, I know that C++ can't deal very well with the situation where the size of a type isn't known at compile time, so I'm willing to accept a layer of indirection:
Type erasure isn't a problem here. Haskell has both type erasure and existential types.
The real problem is that, if foo is a generic container, then an efficient implementation of the existential type exists T. foo<T> needs two things that C++ doesn't have and can't possibly have without significantly changing the language's design:
T's vtable must contain information about T's size and alignment. (Alternatively, we could box all values like Haskell does. But of course that's unacceptable in C++.) Moreover, the representation of foo<T> must be an easily computable function of T's size and alignment. (Template specialization and SFINAE get in the way.)
T's vtable pointer must be stored alongside the container itself, rather than alongside the individual elements. In particular, an object of type exists T. foo<T> always contains one vtable, regardless of the number of elements in the container.
Is that really relevant to OP? What is demonstrated is akin to
class C a where
Β foo :: a -> ()
data Iface = forall a. C a => Iface a
data Data1 = Data1
data Data2 = Data2
instance C Data1 where
Β foo (Data1) = ()
instance C Data2 where
Β foo (Data2) = ()
instance C Iface where
Β foo (Iface i) = foo i
I'm pretty confident it's not possible to store a single vtable for a hypothetical [Iface (Data1), Iface (Data2)] in general. It is possible to do when vector is const and is constructed from objects of the same "real" type, but in that case you may as well use said real type as vector's template parameter.
Dude why are you typing all functions like: "auto func (args) -> return-type { ... }" instead of just "return-type fun (args) { .. }"?
Just honestly curious, what is the benefit of both typing more and having more symbols to look at a later point? You are not the only one, I see some other people type function declarations like that too. Is there some benefit with that version I have missed?
For a class classname and typedef/using type classtype within that class, a trailing return type doesnβt require qualification, i.e. you can write auto classname::f() -> classtype instead of classname::classtype classname::f() Just a little bit of reduced redundancy. Also, code lines up a little neater with auto f() -> T
I liked it until I saw what kind of code it takes to define the Ζ<CanFAndG>. There is such a huge, complex, templated meta-programming machinery behind this example, that maintaining of (or even reasoning about) such code will be very difficult.
In any case, this appears to be a crucial piece of code that calls the actual function:
20
u/germandiago Aug 22 '25
is consteval define_aggregate C++26 syntax?
4
u/not_a_novel_account cmake dev Aug 22 '25
Forgive me, because I am still a novice to reflection syntax, but surely
Membersshould be a union here not a struct? OurQuantifiedTypecan presumably only hold a single possible type, which means we want the storage to overlap when possible no?174
u/PrimozDelux Aug 22 '25
Sorry you don't get to just drop Ζ into a code snippet like it's nothing
85
u/johannes1971 Aug 22 '25
For the people that don't know, an "existential type" is just an existoid in the category of endo-existors.
...
I have no idea what it is.
35
u/[deleted] Aug 22 '25
Love the example shown, hate the naming of "Exist" alias "Ζ"
47
u/Fancy_Status2522 Aug 22 '25
I will check it out in 20 years unless I get out off of embedded
4
u/ContDiArco Aug 22 '25 edited Aug 22 '25
Thanks! That ist awesome!
Lot of good ideas and great tricks!
4
u/bstamour WG21 | Library Working Group Aug 22 '25
As an ex-Haskeller who occasionally misses having access to existential types, this is so cool!
3
u/GYN-k4H-Q3z-75B Aug 22 '25
Godbolt initially only showed me the stuff starting with include <print> and I was so confused. Then I scrolled up. Holy hell, I love reflection.
6
u/Internal-Sun-6476 Aug 22 '25 edited Aug 22 '25
Um. Ow. I'm hating the static cast to int.... but Ok. What the hell is the reverse E. Is that just reddit representation for a reflection/splice.
Further, the CanFAndG is a concept? I did not know you could do that with that syntax.
2
u/positivcheg Aug 22 '25
Iβm a little bit puzzled. How does it work? Does it do some kind of boxing like C# does or does it work like a std::variant?
2
u/Regg42 Aug 22 '25
Alien syntax in proxy π , i don't know what's more confuse in that lib, the syntax, the lib purpose, the lib itself
2
u/RoyAwesome Aug 22 '25
This is awesome.
I'm kinda noodling on a "Reflecting Concepts" idea/proposal to remove the need to create the CanFAndG struct, and instead using concepts to indicate that functions exist on a type and generate a vtable for just those functions. It's cool you got this working without that.
The ability to use concepts as template parameters in cpp26 will make this much easier.
2
u/0xdeedfeed Aug 23 '25
okay random question, are modules cool now in most C++ compilers?
1
u/Choperello Aug 22 '25
Cool so I'll get to use it in prod in about 10 years.
1
u/reflexive-polytope Aug 23 '25
Now do
exists T. vector<T>.1
u/Bemteb Aug 23 '25
Me, still working with C++11 in most projects:
1
u/arthurno1 Aug 24 '25
Dude why are you typing all functions like: "auto func (args) -> return-type { ... }" instead of just "return-type fun (args) { .. }"?
Just honestly curious, what is the benefit of both typing more and having more symbols to look at a later point? You are not the only one, I see some other people type function declarations like that too. Is there some benefit with that version I have missed?
1
u/ContDiArco Aug 25 '25
I wonder, If you could avoid the
"std::any* Object;"
pointer with some use of "no_unique_address" or union tricks...
1
u/LeonardAFX Aug 29 '25
I liked it until I saw what kind of code it takes to define the
Ζ<CanFAndG>. There is such a huge, complex, templated meta-programming machinery behind this example, that maintaining of (or even reasoning about) such code will be very difficult.In any case, this appears to be a crucial piece of code that calls the actual function:
I'm only guessing that calling the functions this way is still
O(1).1
u/MerlinsArchitect Jan 23 '26
This is very cool!
1
u/zerhud Aug 22 '25
How do you type the reverse E?
1
u/LegendaryMauricius Aug 22 '25
How are these allocated in memory? Surely A and B can be of different sizes?
Also what's the point of 'using' instead of normal struct declaration?
Other that this I love it, these are basically interfaces.