Hi There!
I ran into some troubles understanding how type bound operator
overloading works. This is hard to explain so I wort a little example
program.
It starts with a base type which got the '==' operator. As a second step
there is an extension with the '==' operator overloaded. Just have a
look to the attached code. Finally I want to compare types and classes
of:
* base == base
* base == extension
* extension == base
* extension == extension
The issue is that I don not know if the attached code is valid Fortran.
Some compilers as intel and nag are doing fine others as pgi and
gfortran do not.
> Error: 'eq_extension' and 'eq_base' for GENERIC '==' at (1) are
> ambiguous
Could you help me out to understand how operator overloading works
properly?
Many thanks in advance!
Cheers, Stefan
> MODULE my_mod
>
> TYPE :: base
> INTEGER :: a
> CONTAINS
> PROCEDURE, PRIVATE :: eq_base
> GENERIC, PUBLIC :: OPERATOR(==) => eq_base
> END TYPE
>
> TYPE, EXTENDS(base) :: extension
> INTEGER :: b
> CONTAINS
> PROCEDURE, PRIVATE :: eq_extension
> GENERIC, PUBLIC :: OPERATOR(==) => eq_extension
> END TYPE
>
> CONTAINS
>
> ELEMENTAL LOGICAL FUNCTION eq_base(c1, c2)
> CLASS(base), INTENT(IN) :: c1, c2
> eq_base = .FALSE.
> IF (c1%a/=c2%a) RETURN
> eq_base = .TRUE.
> END FUNCTION
>
> ELEMENTAL LOGICAL FUNCTION eq_extension(c1, c2)
> CLASS(extension), INTENT(IN) :: c1, c2
> eq_extension = .FALSE.
> IF (.NOT.(c1%base==c2%base)) RETURN
> IF (c1%b/=c2%b) RETURN
> eq_extension = .TRUE.
> END FUNCTION
>
> END MODULE
>
> PROGRAM main
>
> USE my_mod
>
> TYPE(base) :: b = base(1)
> TYPE(extension) :: e = extension(1,2)
>
> PRINT *, b == b ! => T
> PRINT *, base(2) == e ! => F
> PRINT *, e == base(2) ! => F
> PRINT *, e == e ! => T
>
> END PROGRAM
|