Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22018

Is there a way to avoid preprocessor macros when taking an argument from the command line?

$
0
0

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)


Viewing all articles
Browse latest Browse all 22018

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>