I am writing a library that needs to do some compile time calculations, and builds an array of compile time constants. The issue is I need a way to specify the max size of this array... The only way I know of is to make it a configurable option passed to the compiler.
Then you can use it with preprocessor directives for example:
#ifndef MAX_SIZE
constexpr auto maxSize = 42; // Some default value if no MAX_SIZE is specified
#else
constexpr auto maxSize = MAX_SIZE;
#endif
To set the max size when compiling with gcc, you can compile the code with the option -DMAX_SIZE=<desired_size>
.
The issue I have with this is it involves using preprocessor macros to get the MAX_SIZE
argument from the command line. Preprocessor macros are considered evil for many reasons (that I will not get into here because that isn't the point of the question).
Is there any way to achieve this functionality without using preprocessor macros? (I have up to C++20 available so feel free to go wild with your solutions -- well mostly, some of it isn't implemented yet by gcc 10)