Skip to content

Instantly share code, notes, and snippets.

@certik
Created November 11, 2023 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save certik/e7a9f8c2d945dd8d50f2f27697e862ec to your computer and use it in GitHub Desktop.
Save certik/e7a9f8c2d945dd8d50f2f27697e862ec to your computer and use it in GitHub Desktop.
program XX
requirement is_binary_op(T, op)
type, deferred :: T
function op(lhs, rhs) result(res)
type(T), intent(in) :: lhs, rhs
type(T) :: res
end function
end requirement
TEMPLATE my_template(T, OP)
TYPE, DEFERRED :: T
REQUIRE :: is_binary_op(T, OP)
CONTAINS
function reduce(x) result(y)
TYPE(T) :: y
TYPE(T), INTENT(IN) :: x(:)
INTEGER :: i
y = x(1)
DO i = 2, SIZE(x)
y = OP(y, x(i))
END DO
END function reduce
END TEMPLATE
INSTANTIATE my_template(REAL, OPERATOR(+))
!INSTANTIATE MY_TEMPLATE(MY_T, MY_PLUS)
real :: x(4)
real :: y
x = [1., 2., 3., 4.]
y = reduce(x)
print *, x
print *, y
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment