The usage of default arguments for pointers in C++ can be demonstrated with following code
#include <iostream>
void myfunc_(int var, double* arr = 0) {
if (arr == 0) {
std::cout << "1 arg" << std::endl;
} else {
std::cout << "2 arg" << std::endl;
}
}
int main() {
myfunc(1);
double *arr = new double[2];
myfunc(1, arr);
}
in this case, the output of the program is
1 arg
2 arg
on the other hand, if i try to pass optional arguments from Fortran to C++, it does not work. The following example code demonstrates the sutiation
The myfunc.cpp
#include <iostream>
extern "C" void myfunc_(int var, double* arr = 0) {
if (arr == 0) {
std::cout << "1 arg" << std::endl;
} else {
std::cout << "2 arg" << std::endl;
}
}
and the Fortran main program
program main
use iso_c_binding
real*8 :: arr(2)
call myfunc(1)
call myfunc(1, arr)
end program main
and the mixed code (Fortran and C++) can be compiled using following command without any error
g++ -c myfunc.cpp
gfortran -o main.x myfunc.o main.f90 -lstdc++ -lc++
but the program prints
2 arg
2 arg
in this case. So, Is there any solution for this problem? Am i missing something in here? I think that using default arguments in mixed programming is not working as it expected but i need suggestion at this point.