C++ Function Template Parameters #1093
Replies: 4 comments 4 replies
-
This is possible with C++, see here in the Compiler Explorer demonstrated. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@thestumbler Is it that library: https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h ? There is a "utility" folder with two files "w5100.h" and "w5100.cpp". As far as I can see those are the only ones with a direct dependency on the hardware. All other source files access the device through a global variable declared as Of course all the other Arduino library dependencies have to be dealt with as well. |
Beta Was this translation helpful? Give feedback.
-
To question one: Here is a minimal example for non-type template parameters used with a function template. Not sure, if this will be helpful for your problem. |
Beta Was this translation helpful? Give feedback.
-
(I will probably make some mistakes with the terminology in this post, as I am new to some of these concepts and still learning by example.)
I understand on the surface how MODM specifies compile-time parameters using function template parameters. Some of the results are boggling like the UART speed calculations. Let's consider a simpler example, like the Chip Select parameter used in a few of the register access functions of the recently added SPI / DMA example for the Nucleo H723 board. I can wrap my head around this, and it makes sense.
I am porting some C++ code for the Wiznet W5100s's Arduino library into MODM. This approach is a compromise between (1) the resulting kludge if you tried to shoehorn Wiznet's C library into MODM, and (2) the daunting task of rewriting the entire large library of functions from scratch.
The Arduino C++ library is organized so that the hardware specific interactions are not too difficult to swap out (actually the Wiznet C library is a bit better in that regard). One thing they did was to generate a dozen or so pairs of functions for reading / writing each register. For example,
readMR()
andwriteMR()
, etc. Thess repetitive function definitions are done using the preprocessor. I'm not one to shy away from cryptic preprocessor incantations, but only when I can't find a "normal" way to accomplish the task. This method seems out of place in a C++ program.I tried to replace this approach with function template parameters, like the above mentioned example. It was a steep learning curve, but I tentatively determined that my goal was impossible. When you pass something like GyroCs as a function parameter, that is a class known at compile time. In my case, I wanted to pass a certain register instead. For example,
AccRegister::Status
. As it took me some time to discover, you can't do that. I think.I ended up passing the register name at runtime to a handful of functions, equivalent to the original Arduino code's preprocessor macros. And in hindsight, this is probably good enough.
Question 1: in one search result on this topic, I read that in C++23(?) you can now use certain non-class, constant parameters to a function template. I tried briefly and failed, so I ran with a different approach. Are there any such examples in MODM of passing a non-class parameter?
As everyone says, C++ compiler error messages are a pain to read. My normal flow has been to pipe them to
junk.cpp
and view that in my editor (being sure to delete this file before rebuilding). I discovered that VSCode has slightly easier to read error messages. But, the main focus of my edits are in MODM itself because I'm basically adding a new driver to the ETHERNET group. I'm not a daily VsCode user, and setting up a multi-project solution is non-trivial for me.Question 2: Are there any examples of a MODM application in VsCode that deals with the separate nature of the MODM library and the LBUILD BUILD step?
Beta Was this translation helpful? Give feedback.
All reactions