Below that post of mine, Chris Elrod posted another version of the same thing which is more elegant (in both languages), although the Fortran implementation in his case is directly thought to be interfaced with Julia, so some syntax is not natural for Fortran in general.