Re: Ambiguous interfaces
From: Rich Townsend (rhdt_at_barVOIDtol.udel.edu)
Date: 03/02/04
- Next message: Michel OLAGNON: "Re: appending to a string"
- Previous message: Jugoslav Dujic: "Re: appending to a string"
- In reply to: Stig Kildegård Andersen: "Ambiguous interfaces"
- Next in thread: glen herrmannsfeldt: "Re: Ambiguous interfaces"
- Reply: glen herrmannsfeldt: "Re: Ambiguous interfaces"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 02 Mar 2004 10:25:09 -0500
Stig Kildegård Andersen wrote:
> Hello all,
>
> I have a question about ambiguous interfaces. Is the following code standard
> conforming or broken, if kind number 8 is supported by the compiler and if
> it is the deault integer kind?
>
> If the code is broken (I assume that it is): Is there any other way to use
> generic interfaces for subroutines that operate on different kinds of
> integers, where, in some cases, the kind numbers are identical?
>
> Kind Regards and Thank You in advance,
> Stig Kildegård Andersen
>
> !-------------
>
> module MyModule
> implicit none
>
> integer, parameter :: BigIntegerKind = 8
>
> interface DoSomething
> module procedure DoSomething_DefaultInteger
> module procedure DoSomething_BigInteger
> end interface
>
> contains
>
> subroutine DoSomething_DefaultInteger(argument)
> integer, intent(in) :: argument
> write(*,*) argument
> end subroutine
>
> subroutine DoSomething_BigInteger(argument)
> integer(kind=BigIntegerKind), intent(in) :: argument
> write(*,*) argument
> end subroutine
>
> end module
>
> !-------------
>
> program ambiguous
> use MyModule
> implicit none
>
> integer :: MyInteger = 0
>
> write(*,*) kind(1), kind(1_BigIntegerKind)
> call DoSomething(MyInteger)
>
> end program
>
Apart from the fact that you should try to avoid using hard-coded kind
parameters (use SELECTED_*_KIND instead), you are correct in your
supposition. Basically, if you combine two or more specific routines
into a generic routines, and the kind parameters happen to be identical
(in a platform-dependent sort of way), then your program becomes
non-conforming.
This is something I've thought long and hard about. In many of my
libraries, I have generic interfaces to routines which operate at
default real precision and at double precision. However, on a 64-bit
platform the double precision routines may well end up as 128-bit
floats, which is overkill. Therefore, it would be better to program my
"single" and "double" precision specific routines as "32-bit real" and
"64-bit real" precision routines, by use of appropriate
SELECTED_REAL_KIND calls.
However, then I run into the problem Stig has pointed out. What then
happens when on a 64-bit platform SELECTED_REAL_KIND assigns me the same
kind paramter for my 32-bit real and 64-bit real routines? Then, my
routines will appear identical when combined into a generic, and my
program won't be conforming anymore.
The essence of the problem is that SELECTED_REAL_KIND gives a kind
parameter with at least the precision/range requested, but there is no
hard upper limit on the precision/range -- only, AFAIK, a requirement to
chose the one with the least decimal precision, if more than one kind is
available.
The only way I've come up with to solve this "kind collision" problem is
through conditional compilation. Anyone else have any thoughts?
cheers,
Rich
PS Yes, I know that I shouldn't be talking about 32-bit and 64-bit, but
instead should be referring to the range etc of my number models. But
the bit nomenclature is easier to remember...
PPS Its worth mentioning that I've never encountered this kind collision
in real-world situations; but its also worth mentioning that I'm only
just starting to port my libraries to 64-bit platforms.
- Next message: Michel OLAGNON: "Re: appending to a string"
- Previous message: Jugoslav Dujic: "Re: appending to a string"
- In reply to: Stig Kildegård Andersen: "Ambiguous interfaces"
- Next in thread: glen herrmannsfeldt: "Re: Ambiguous interfaces"
- Reply: glen herrmannsfeldt: "Re: Ambiguous interfaces"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|