Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29252 Discussions

Comparison operator overridden in extending types

Bálint_A_
Beginner
718 Views

Dear All,

I have some problems with the == operator when using Intel 15. Below the self containing example demonstrating the issue. IMHO, the comparison between instances of the derived types calls the wrong routine (the one of the base type instead of the overriden one of the extending type), when the instances are accessed via class pointers of the base type. Other compilers do it differently, and although I do not have a detailed knowledge of the standard, the behaviour feels incorrect. Any views on that?

Best regards,

Bálint

module testmod
  implicit none

  type :: Base
  contains
    procedure :: isEqualTo => Base_isEqualTo
    generic :: operator(==) => isEqualTo
  end type Base

  type, extends(Base) :: Extended
    integer :: ii
  contains
    procedure :: isEqualTo => Extended_isEqualTo
  end type Extended

contains

  function Base_isEqualTo(this, other) result(isEqual)
    class(Base), intent(in) :: this
    class(Base), intent(in) :: other
    logical :: isEqual

    print *, "Base_isEqualTo invoked"
    isEqual = .false.

  end function Base_isEqualTo


  function Extended_isEqualTo(this, other) result(isEqual)
    class(Extended), intent(in) :: this
    class(Base), intent(in) :: other
    logical :: isEqual

    print *, "Extended_isEqualTo invoked"
    select type (other)
    class is (Extended)
      isEqual = (this%ii == other%ii)
    class default
      isEqual = .false.
    end select

  end function Extended_isEqualTo

end module testmod


program test
  use testmod
  implicit none

  type(Extended), target :: ext1, ext2
  class(Base), pointer :: pBase1, pBase2
  logical :: isEqual

  ext1%ii = 1
  ext2%ii = 1
  pBase1 => ext1
  pBase2 => ext2
  isEqual = (pBase1 == pBase2)
  print *, "Equal?", isEqual

end program test
0 Kudos
1 Solution
FortranFan
Honored Contributor III
718 Views

The code seems to work as expected using Intel Fortran compiler 16.0, initial release.  Perhaps you can consider upgrading your compiler?

View solution in original post

0 Kudos
2 Replies
FortranFan
Honored Contributor III
719 Views

The code seems to work as expected using Intel Fortran compiler 16.0, initial release.  Perhaps you can consider upgrading your compiler?

0 Kudos
Bálint_A_
Beginner
718 Views

Thanks for testing it. Apparently, I'll have to upgrade then. :-)
 

0 Kudos
Reply