Hello. Sorry if this question has been asked before, I tried searching around and couldn’t find it.
As the title suggests, I am wondering about the best practice for conditionally defining datatypes known at compile time. In my case, I have some code that can be executed identically for real or complex input data. Most of the time the input will just be real data, but I want to add complex input functionality. The code does the same operations whether or not the input data is real or complex, but for performance reasons I would like real to only be handled as real and only define complex as needed. Before the code is run, the input is known to be real or complex, so my thought is to just have a compiler flag that says whether or not to compile using real or complex in the following fashion:
program dtype_test
implicit none
#ifdef CPLX
#define DTYPE COMPLEX
#else
#define DTYPE REAL
#endif
DTYPE :: x
x = (2.5, 3.1)
print *, x
endprogram dtype_test
Which compiles and runs as expected for me entering ifx dtype_test.f90 -fpp
(though the Modern Fortran vscode linter complains).
Alternatively I could have had #ifdef statements for each variable I declare, but that would result in roughly twice as many lines of code (one for complex, one for real declarations) when I could just write DTYPE as appropriate.
Anyways, I just wanted to know if there is a generally accepted ‘better’ approach to this type of conditional compilation, or if there is some other better approach that I haven’t thought of.