Overloading the `int` intrinsic function to take `logical` inputs

I would like to overload the int intrinsic function so that int(.true.) = 1 and int(.false.) = 0. Here is my implementation. Is it correct and standard-compliant? I have never tried overloading intrinsic functions. Thank you for suggestions and comments.

module int_mod
implicit none
public :: int

interface int                                                                                                                        
      module procedure logical_to_int                                                                                                  
end interface int 


pure elemental function logical_to_int(x) result(y)                                                                                  
implicit none                                                                                                                        
logical, intent(in) :: x                                                                                                             
integer :: y                                                                                                                     
y = merge(tsource=1, fsource=0, mask=x)                                                                                        
end function logical_to_int           

end module int_mod                                                                                                         

Yes, overloading intrinsic functions is standard compliant. An example from one of my libraries:


Why some of these are not standard is puzzling; but some other interesting ones are to allow
int() and real() to take numeric strings as well as logicals, to allow using == and and /= with logicals instead of .EQV. and .NEQV., too change SIGN so it only requires a single value, allow merge() to take scalar strings of different lengths, and to allow the // operator to take numeric values and convert them to strings.


I totally agree that it would be nice to have int and real taking strings and logical values intrinsically. Regarding == and /=, it is a bit unexpected to me that they do not handle logicals, even though strings are accepted.

Several compilers allow == and =/ with logicals, but it has never been standardized. There have been several discussions regarding this and some dusty corners regarding operator precedence that I was trying to find to link to, but did not find them with a cursory scan. Functions for converting between numeric values and strings is probably the most “reinvented wheel” in Fortran, and I have felt extending int() and real() was a natural place for them, but there are some issues about what to do with error conditions; but there are some of the same issues with overflow (like doing an int(too_big_a_float_to_fit_in_the_integer_kind) that are not exactly handled gracefully either with those same functions.

A few compilers supply the missing “CHARACTER()” function to convert numeric values to strings too, which seems a nice name for such a function.

1 Like