C++ でのポインターのデフォルト引数の使用法は、次のコードで実証できます。
#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);
}
この場合、プログラムの出力は
1 arg
2 arg
一方、オプションの引数を Fortran から C++ に渡そうとしても、うまくいきません。次のコード例は、状況を示しています
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;
}
}
およびFortran メインプログラム
program main
use iso_c_binding
real*8 :: arr(2)
call myfunc(1)
call myfunc(1, arr)
end program main
混合コード (Fortran と C++) は、次のコマンドを使用してエラーなしでコンパイルできます。
g++ -c myfunc.cpp
gfortran -o main.x myfunc.o main.f90 -lstdc++ -lc++
しかし、プログラムは印刷します
2 arg
2 arg
この場合。それで、この問題の解決策はありますか?ここで何かが足りないのですか?混合プログラミングでデフォルト引数を使用すると、期待どおりに機能しないと思いますが、この時点で提案が必要です。