Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

overloading and optional args

forall
Beginner
442 Views
is it legal to overload 2 procedures as follows?

the potential ambiguity in the call here is resolved by the use of the keywords (required by construction in this example)

CVF and IVF dont give any compiler warnings and execute fine. However, IVF does crash if debugging by stepping 1 line at a time into len_trim (but not if placing breakpoint inside len_trim2 and F5'ing to it). CVF debugs fine.

!******************************************************************
module strings
implicit none
!---
interface len_trim
module procedure len_trim2
endinterface len_trim
!---
contains
!----------------------------------------------------
elemental function len_trim2(string1,string2)
implicit none
character(*),intent(in),optional::string1
character(*),intent(in)::string2
integer(4)::len_trim2
if(present(string1))then
len_trim2=len_trim(string1)
else
len_trim2=len_trim(string2)
endif
endfunction len_trim2
!----------------------------------------------------
endmodule strings
!******************************************************************
program main
use strings
implicit none
character(*),parameter:: str='string'
write(*,*)len_trim(string=str) ! invokes intrinsic
write(*,*)len_trim(string2=str) ! invokes defined len_trim1
endprogram main
!******************************************************************

Message Edited by forall on 04-20-2005 12:03 PM

0 Kudos
1 Reply
Steven_L_Intel1
Employee
442 Views
Note that it is the first argument to len_trim2 that is optional. If it were the second, then this would be ambiguous. Because it is the first, and the second non-optional argument does not exist in the intrinsic, then this is ok. Note that there is no way to pass the second argument to len_trim2 without using the keyword.

The code runs ok for me, though I didn't try setting a breakpoint.
0 Kudos
Reply