Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

OOP function overriding bug

$
0
0

Hi all,

When using a generic operator in an extended type, the operator binding is not added to the list from the ' mother' type. The compiler reject the code saying the operation is invalid. If you remove the operator in the extend type, everything is fine.

Also, I'm testing overriding feature, and I do not really understand why function overriding is working but not when using the operator.

 

Thanks.

 

module test
type  :: my_integer
                integer, private :: value
        contains
                procedure,pass :: add_r => add_my_integer_r
                generic,public :: operator(+) => add_r
end type my_integer
type, extends(my_integer) :: my_integerr
        contains
                procedure,pass :: add_r => add_my_integer_rr
                procedure,pass :: add_real
                generic,public :: operator(+) => add_real
end type my_integerr

contains
function add_real(a,b) result(r)
        class(my_integerr),intent(in) :: a
        real,intent(in) :: b
        real :: r
        r=a%value+4d0*b
end function
function add_my_integer_rr(a,b) result(r)
        class(my_integerr),intent(in) :: a
        integer,intent(in) :: b
        integer :: r
        r=a%value+3*b
        print *,'overriding function'
end function
function add_my_integer_r(a,b) result(r)
        class(my_integer),intent(in) :: a
        integer,intent(in) :: b
        integer :: r
        r=a%value+b
end function
end module test
program toto
        use test
class(my_integer),allocatable :: t
class(my_integerr),allocatable :: tt
type(my_integerr),allocatable :: ttt
integer :: a

allocate(t,tt,ttt)
a=2
print *,'with operator'
print *,t+a
print *,tt+a
print *,ttt+a
print *,ttt+2.0

print *,'with function'
print *,t%add_r(a)
print *,tt%add_r(a)
print *,ttt%add_r(a)

end program toto

 

Result in  :

test2.f90(47): error #6355: This binary operation is invalid for this data type.   [TT]
print *,tt+a
--------^
test2.f90(47): error #6549: An arithmetic or LOGICAL type is required in this context.
print *,tt+a
----------^
test2.f90(48): error #6355: This binary operation is invalid for this data type.   [TTT]
print *,ttt+a
--------^
test2.f90(48): error #6549: An arithmetic or LOGICAL type is required in this context.
print *,ttt+a
-----------^
compilation aborted for test2.f90 (code 1)

 

 


Viewing all articles
Browse latest Browse all 3270

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>