C3 0.8.0 replaces builtins, simplifies reflection, and rethinks unsigned sizes
Posted by Nuoji@reddit | programming | View on Reddit | 22 comments
Posted by Nuoji@reddit | programming | View on Reddit | 22 comments
superxpro12@reddit
Does c3 permit/implement virtual polymorphism at compile time?
I'm in the embedded space and desperate for such a capability.
Nuoji@reddit (OP)
What do you want to do more in detail? Give me a code example!
superxpro12@reddit
Basically classic interface programming like in c++. But I work in a resource-constrained environment and I dont want to pay for vtables.
So i want to work with an interface like:
Class UartInterface { Tx_Byte(uint8_t b) }
Class UartImpl_STM32G4xx { TX_Byte(uint8_b) {USART0.tx_register_abcd(b) ... more stm32g4-specifc-stuff }
Class UartImpl_AtmelSAM...
Class UartImpl_NXP_IDK...
And then somewhere in a configuration source file do: UartInterface u = new UartImpl_Atmel_SAM;
And have the compiler/linker not do heaps and gobs of redirections and vtables.
It's all known at compile time. But once you leave a compilation unit, it gives up.
Link-Time-Optimization may be the answer here, idk. I've tried every contemporary model and it always generates vtables.
And i dont want to use templates. Yuck.
Ameisen@reddit
The C++ ways to do this would be to either use a type alias, or to use template-driven static inheritance.
superxpro12@reddit
Youre not wrong. But I pine for a world in which I dont have to rely on a language feature thats essentially copy-pasting classes via templates. I know they are A solution, but virtual constexpr's get sooo close, and fumble the ball at the 1yd line.
Nuoji@reddit (OP)
So do the UartImpl_AtmelSAM add data fields on top of what UartInterface contains? Or does it only add methods?
superxpro12@reddit
They're all implementations of the base class UartInterface. They each provide their own implementation of the pure virtual TX_Byte function.
Nuoji@reddit (OP)
But data fields are the same and just differs in methods? And then only one of these implementations is actually used across the codebase?
I see at least three ways to do it if that's the case, with the last one doing virtual dispatch, so you probably don't want it. But it's a simple solution that should work in C++ and C as well.
superxpro12@reddit
in c++, this usually breaks when you jump between translation units.
I've read thru Scott Meyer's Presentation on Embedded C++ a few times now, as well as Beningo's course on the same topic. They both go into various solutions, but they all have drawbacks.
You can try header-only stuff but that has downsides.
I really want to develop all my hardware abstractions using an interface class, and then plug that with a specified implementation.
But i dont want to use templates, I dont want to use vtables, and i want to keep the type safety.
Nuoji@reddit (OP)
What I don't understand is why you can't do
superxpro12@reddit
You can! But the preprocessor feels.... Wrong. Text substitution.
Sure it works but it feels more like a hack than a supported feature imo.
I want the ability to choose the class used at compile time through assignment, not by rewriting all the text of the source code.
Ameisen@reddit
typedef/usingare not preprocessor directives - they're language keywords.superxpro12@reddit
I agree. That isnt, but the preprocessor macros before it are.
Nuoji@reddit (OP)
That should be simple enough to do in C3
Ameisen@reddit
Type aliases like that or using
template-driven static inheritance are the correct solutions to this in C++.They seem opposed to both for some reason, though.
Nuoji@reddit (OP)
Oops, well that's what happens when you just type it on the phone.
mido0o0o@reddit
Can I pick C3 without knowing C? I use multiple other languages and of course I know the c syntax but never actual used C
Nuoji@reddit (OP)
Yes, although it's similar to what you'd have to use C. There are conveniences to get you much further earlier though. But an understanding of pointers and memory is necessary.
UloPe@reddit
So a better horse?
Nuoji@reddit (OP)
There are some sleeper features.
ReallySuperName@reddit
How well does C3 fit into your existing or new applications that need to use other C compilers? The homepage says it fits right in, but how much? I'd love to use this on top of C compilers I use for embedded development for microcontrollers, for example.
Nuoji@reddit (OP)
You can produce .o files which will link and integrate with .o files from C programs. So for example if it's a make file you'd invoke the C3 compiler with `compile-only` and get the relevant object files, and then for the C files you use the C compiler as usual, finally linking them together in the normal way.
By the time of 1.0 it should be possible to compile to C files as an alternative. This is obviously better if you want to target platforms that aren't yet available.