Created
November 11, 2023 00:31
-
-
Save certik/e7a9f8c2d945dd8d50f2f27697e862ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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