3

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

在这种情况下。那么,有没有解决这个问题的方法?我在这里错过了什么吗?我认为在混合编程中使用默认参数并没有按预期工作,但此时我需要建议。

4

1 回答 1

2

正如评论中所指出的,您不能在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界面中的属性。如果您不提供可选数组,则会传递一个空指针。

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

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

于 2016-04-07T09:13:06.283 回答