dimanche 29 juillet 2018

Compute constant in header file at compile time

I have a header file that contains functions based upon the user OS, it does so using:

#ifdef _WIN32 // Windows
...
#else // Linux/Unix code (I know it will be either Windows or Linux/Unix)
...
#endif

The functions defined in their appropriate blocks are currently called from main at runtime and store a constant, but this got me thinking: Can I compute this constant in the header at compile?

Something like:

#ifdef _WIN32 
// function here; call it foobar()
#define WINCONST foobar()
#else
// function here; call it xfoobar()
#define NIXCONST xfoobar()
#endif

However I am not sure that you can use function calls in the #define preprocessor directive. I know that you can use it in a manner such as #define ADD(x, y) (x + y) but thats about it.

Aucun commentaire:

Enregistrer un commentaire