C++和混合编程中指针的默认参数

Default arguments for pointer in C++ and mixed programming

本文关键字:默认 参数 指针 混合 编程 C++      更新时间:2023-10-16

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

在这种情况下。那么,这个问题有什么解决办法吗?我是不是错过了什么?我认为在混合编程中使用默认参数并不像预期的那样有效,但在这一点上我需要建议。

正如注释中所指出的,extern "C"函数中不能有参数的默认值。但是,您可以在函数的Fortran接口中有可选参数,并按照您想要的方式调用它:

#include <iostream>
extern "C" void myfunc(int var, double* arr) { //removed the = 0 and renamed to myfunc
  if (arr == 0) {
    std::cout << "1 arg" << std::endl;
  } else {
    std::cout << "2 arg" << std::endl;
  }
}

在Fortran中创建一个描述C(C++)函数的接口:

program main
use iso_c_binding
interface
  subroutine myfunc(var, arr) bind(C, name="myfunc")  
    use iso_c_binding
    integer(c_int), value :: var
    real(c_double), optional :: arr(*)
  end subroutine
end interface
!C_double comes from iso_c_binding
real(c_double):: arr(2)
call myfunc(1_c_int)
call myfunc(1_c_int, arr)
end program main

关键是接口中的optional属性。如果不提供可选数组,则会传递一个空指针。

还要注意var参数的value属性。这是必要的,因为C++函数按值接受参数。

注意:这是Fortran 2008标准中最近添加的TS,但得到了广泛支持。