STL analogue in Fortran

There are no templates in Fortran and hence no STL. You can try FLIBS for some generic libraries. It generally uses transfer() tricks to achieve generic programming.

There is a preprocessor which adds some templates to Fortran and comes with some small STL, you can try that too named PyF95++. If you have access to academic papers through some library, you can read about it here.

I would avoid mixing it with C++ in this specific case although it can be done. You must instantiate each case separately and interface it to Fortran using a wrapper (bind(C) and iso_c_binding). Only if you have a very limited number of types you want to use the algorithms for it could be worth it.

You can also try to implement some poor-man’s templates using the C-preprocessors in Fortran, For smaller libraries it works, but can become too difficult to maintain or ugly for complex things. As an example you can see my implementation of a linked list https://github.com/LadaF/fortran-list .

Generally, there is no clearly right approach or answer, you always have to choose from more possibilities.

Leave a Comment