and i would like to find the maximum number and set it to another define variable
#define highestNum
I would like this to be done on compile time so how do i find the largest number using a macro? It could be argued that the user might input it themself since they are setting definitions anyway, but i would like to make it so that the user will only define as little variables as possible
The highestNum is going to be used as part of a preprocessor check of a library im using documentation says i have to define it (dont know why not include it in the library and let the user input it). Im also not aware of a way if a macro can use a variable, but im just following the guide
If experts would generalize their, um, expertise it would be hekpful.
What and how much can be done along these lines at compile time?
I once heard of a macro pre-processor so able it could be hacked to play a game of tick-tax-tow with the user who invoked the assembler on it. Dunno if the program it made was significant.
Probably beyond the C/C++ preprocessors, but it reminds me that it can do many things I haven’t even dreamt of.
Not sure you'll call this cleaner or simpler... but thought I would throw that out there for the sake of fun
C++ offers great evaluation (even at compile time) capabilities with constexpr and you can put that to work to your benefit by using templated recursive calls..
here is an example:
template <typename T> constexpr T maximum(T a) {return a ;}
template <typename T> constexpr T maximum(T a, T b) {return a > b ? a : b;}
template <typename T, typename... Targs> constexpr T maximum(T first, Targs... rest) {return maximum(first, maximum(rest...));}
#define numA 300
#define numB 600
#define numC 100
#define numD 200
#define numE 400
#define numF 500
#define numG 42
constexpr int numMax = maximum(numA, numB, numC, numD, numE, numF, numG);
void setup() {
Serial.begin(115200); Serial.println();
Serial.print(F("Max = ")); Serial.println(numMax);
}
void loop() {}
the great thing about this is — as demonstrated above — that you are no longer obliged to modify the macro if you have more or less than 4 values as the maximum() function takes a variable number of arguments ➜ you now can pass any number (1 or more) of constexpr arguments.
The other nice thing about it is the code size impact➜ NO code has been generated. It's evaluated at compile time for you by the compiler because everything is constexpr.
(of course I would discourage you to use the #define and go for constexpr as well for your numA, numB, ...) as strong typing always helps the compiler understand your intent.