Metaprogramming vs. generic programming in Fortran

Sure, fypp is just a dumb preprocessor, so it definitely does not make template/generics in Fortran superfluous.

The only thing it can offer over the current include based templating strategies is, that you can define multiple template instances within one module file and even loop over the types, which should be instantiated.

#:include "listtemplate.fypp"
#:set LISTS = [("list_int_t", "integer"), ("list_real_t", "real"), ("list_logical_t", "logical")]

module mylists
  implicit none

  private
  #:for NAME, _ in LISTS
    public :: ${NAME}$
  #:endfor

  #:for NAME, TYPE in LISTS
    @:declare_list(name=${NAME}$, itemtype=${TYPE}$)
  #:endfor

contains

  #:for NAME, TYPE in LISTS
    @:implement_list(name=${NAME}$, itemtype=${TYPE}$)
  #:endfor

end module mylists

Whether you consider it as a benefit or not, is of course a matter of taste.

1 Like