I would like to find a way to get a fortran method to take some value for an optional parameter that will make the argument not appear to be present in the method that has been called.
Here is a reduced test case which distills the problem in the fortran code:
MODULE FortranOptional
USE ISO_C_BINDING
CONTAINS
SUBROUTINE optionaltest(scalar)
REAL(8), OPTIONAL, INTENT(IN) :: scalar
IF (PRESENT(scalar)) THEN
print *, "scalar is present: ", scalar
ELSE
print *, "scalar is NOT present"
END IF
END SUBROUTINE optionaltest
SUBROUTINE optionaltest_c(scalarC) BIND(C, NAME="optionaltest_c")
REAL(C_DOUBLE), OPTIONAL, INTENT(IN) :: scalarC
REAL(8) :: scalar
IF (PRESENT(scalarC)) THEN
print *, "scalarC is present: ", scalarC
scalar = scalarC
ELSE
print *, "scalarC is NOT present"
! Can I do something here to make scalar appear not present in optionaltest()?
END IF
CALL optionaltest(scalar)
END SUBROUTINE optionaltest_c
END MODULE FortranOptional
And the associated C++ test code:
extern "C"
{
void optionaltest_c(double*);
}
void testMethod()
{
double v = 5.3;
optionaltest_c(0);
std::cout << "\n";
optionaltest_c(&v);
}
Which produces:
scalarC is NOT present
scalar is present: 6.9118029901527309E-310
scalarC is present: 5.2999999999999998
scalar is present: 5.2999999999999998
Is there any way I can set the scalar
variable based on the presence of scalarC
that will make it appear to not be present when scalarC
is not present?
Some constraints:
I'd prefer to avoid if statements involving different calls to
optionaltest
, as the real version of the subroutine has several optional parameters that may be specified or not on an individual basis, resulting in a combinatorial explosion.I cannot bind the
optionaltest
subroutine directly as a C function, as the real version of the subroutine has assumed shape optional arguments, which are not C-compatible in the version of Fortran I am using.I cannot modify the signature of
optionaltest
.
CALL optionaltest(scalar)
in subroutineoptionaltest_c
possible, or are you having (perhaps because of different kind/type) that to havescalar
andscalarC
distinct variables? – francescalusCALL optionaltest(scalar)
does appear inoptionaltest_c
; did you meanCALL optionaltest(scalarC)
? I cannot do the latter since, as you guessed, scalar and scalarC are not quite the same and I can't just passscalarC
through. – professional_yet_not_trackablescalarC
. For interest (or best possible answer), can you state compiler and version? As you've noted we're in areas of variable support. – francescalus