Page 1 of 1

C++ Templates based common devices

Posted: Mon Mar 06, 2023 6:55 am
by ag123
I'm just thinking aloud.
I think we can maintain a separate tree (in a separate repository, quite like a 'library') for c++ templates based common devices e.g. spi, uart, i2c, adc etc, they wouldn't fit right in in the Arduino API, but can be deemed an alternative API. This would allow us to experiment with ideas. And maybe at some point if it turns out it is mature and that they can be integrated, perhaps they could be merged 'in future'.

A good thing about c++ templates, say vs pure virtual classes is that templates are just like #defines, i.e. that they do not consume additional flash and memory and is a standard c++ feature.

using c++ templates would allow alternative implementations, e.g. with DMA vs 'vanilla' implementations which may be just similar with the SPI api, etc.
And that given the separation of templates and implementations, it may make possible implementations say by stm32 series, hence allowing for leaner codes and binaries instead of patching if-defs to cater for cross series differences.
Normally, pure virtual classes and implementations does the job, but that pure virtual classes consume some flash and sram even for the 'empty' stub.
I'm not sure if templates would consume extra flash and ram though, even though the concept of templates is they work 'like macros'

incidentally, i think even 'variants' can be defined with templates.

well, this idea is 'not tested', but that c++ templates may be deemed a solution sort of. and that it would detour from 'Arduino' api as that is more 'C' like. A facade can be nevertheless made for it, but I think the syntax and declarations part would at least be different.

Re: C++ Templates based common devices

Posted: Tue Mar 21, 2023 3:17 pm
by fpiSTM
Yes, it is a nice idea. I thought about this some years ago but don't have time to POC.

Re: C++ Templates based common devices

Posted: Fri Jun 02, 2023 9:49 am
by jacobli
Actually, C++and Arduino are still very similar.