Here’s a little toy example:
subroutine print_present(opt, name) implicit none real, intent(in), dimension(:), optional :: opt character(len=*), intent(in) :: name print*,name, present(opt) end subroutine print_present
Passing a zero-sized array seems to result in what looks like either undefined behaviour or a bug:
call print_present([real::], "Literal zero-sized array")
Gfortran mostly says this is not
present, but sometimes it does evaluate to true. Intel seems to always evaluate to true.
Passing an unallocated array does seem to always evaluate to false, on both gfortran and Intel compilers. Passing an allocatable array that’s zero-sized does seem to evaluate to true for both compilers. It seems to only be zero-sized literal arrays and unallocated arrays that have issues.
Here’s a godbolt reproducer to have a play around with:
Does anybody know what the standard says here? This definitely feels like a bug to me.