Somewhat Variadic function in Modern Fortran

44 Views Asked by At

I want to implement a logger functionality in some large Fortran code. To do so, I want to provide the users with a simple 1-line function call, lets say log_info(<list of variables>) that accepts an unknown amount of variables from different types and ranks (variadic function). This function will basically do what write(*,*) does, but on a buffer variable that i will manipulate for "pretty print" afterwards: write(log_buffer,*) <list of variables>. Is this possible in Fortran? Because I know Fortran doesn't allow variadic functions, but this is a special case that i want to use the exact functionality of write(*,*) but creating a function that wraps it in order to hide it from the user.

You can imagine a code like this:

function log_info(<list of variables>) result(msg)

character(len=123456789) :: msg
write(msg,*) <list of variables>)

end function 



!!!!!!!!! the user code

log_info('what a great day', 12)
log_info('I wish fortran had variadic')

My friend though of a annoying workaround to run a C++ preprocesser with a variadic #define, and afterward to compile the result.

We have implemented the workaround but it doesn't work with CMake as it requires to execute C preproessor and compiling the result of the preprocessor

0

There are 0 best solutions below