On 10/14/2010 12:20 PM, robin wrote:
> From: Dick Hendrickson Sent: Thursday, 14 October 2010 9:26 AM
>
>> Printing out the KIND of an argument is pretty useless. What would
>> you actually use it for?
>
> You might want to have a hard copy of the available kind numbers.
But that's easier to do via:
$ cat test.f90
use ISO_Fortran_env
write (*,'(a,*(i0:", "))') 'Available REAL kinds: ', real_kinds
end
$ gfortran test.f90 && ./a.out
Available REAL kinds: 4, 8, 10
(Granted - the example uses two Fortran 2008 features.)
On 10/14/2010 05:39 AM, robin wrote:
>> That was of course only an example. One use would be to implement
>> generic
>> functions similar to templates in C++.
>
> You can write your own generic functions in Fortran.
Yes and no. I think Fortran up to Fortran 2008 does not offer a real
means to do generic programming such as C++'s templates do. There are
some features which go into that direction, though. (This includes
generic interfaces [cf. below] but also Fortran 2003's type kind and
length parameters - or Fortran 2008's "type(integer)" as alias for
"integer".)
Regarding generic programming: I would not be surprised if the next
standard ("Fortran 2013") will support generic programming; some step
into that direction was planned for Fortran 2008 (macros) but the
feature was not included in the final standard - admittedly, I do not
know the details of this not-included feature.
However, since Fortran 90 you can create use a generic interface to
bundle manually different specific functions under the same name. It
does not allow "write it once, use it many times", but one the other
hand, each function can be adapted for different needs. Example:
$ cat test2.f90
module m
interface generic
module procedure one
module procedure two
end interface generic
contains
subroutine one (A)
real :: A
print *, 'Single precision value A = ', A
end subroutine one
subroutine two (A)
real(kind(0.0d0)) :: A
print *, 'Double precision value A = ', A
end subroutine two
end module m
use m
call generic(4.0)
call generic(4.0d0)
end
$ gfortran test2.f90 && ./a.out
Single precision value A = 4.0000000
Double precision value A = 4.0000000000000000
Tobias
|