c++ binding example

A function object that, when called, calls fn with its arguments bound to args.

If fn is a pointer to member, the first argument expected by the returned function is an object of the class *fn is a member (or a reference to it, or a pointer to it).

// bind example
#include <iostream>     // std::cout
#include <functional>   // std::bind

// a function: (also works with function object: std::divides<double> my_divide;)
double my_divide (double x, double y) {return x/y;}

struct MyPair {
  double a,b;
  double multiply() {return a*b;}
};

int main () {
  using namespace std::placeholders;    // adds visibility of _1, _2, _3,...

  // binding functions:
  auto fn_five = std::bind (my_divide,10,2);               // returns 10/2
  std::cout << fn_five() << '\n';                          // 5

  auto fn_half = std::bind (my_divide,_1,2);               // returns x/2
  std::cout << fn_half(10) << '\n';                        // 5

  auto fn_invert = std::bind (my_divide,_2,_1);            // returns y/x
  std::cout << fn_invert(10,2) << '\n';                    // 0.2

  auto fn_rounding = std::bind<int> (my_divide,_1,_2);     // returns int(x/y)
  std::cout << fn_rounding(10,3) << '\n';                  // 3

  MyPair ten_two {10,2};

  // binding members:
  auto bound_member_fn = std::bind (&MyPair::multiply,_1); // returns x.multiply()
  std::cout << bound_member_fn(ten_two) << '\n';           // 20

  auto bound_member_data = std::bind (&MyPair::a,ten_two); // returns ten_two.a
  std::cout << bound_member_data() << '\n';                // 10

  return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fortran 调用 C++ 动态库可以分为以下步骤: 1. 编写 C++ 代码,并将需要导出的函数声明为 `extern "C"`,以便 Fortran 可以调用。 2. 将 C++ 代码编译成动态库,生成 .so 文件。 3. 在 Fortran 代码中声明需要调用的 C++ 函数,使用 `external` 关键字声明函数名、参数列表和返回值类型。 4. 在 Fortran 代码中使用 `iso_c_binding` 模块中的 `c_f_pointer()` 函数将 C++ 函数的指针转换为 Fortran 中的指针。 5. 在 Fortran 代码中调用 C++ 函数,使用 `call` 语句调用。 以下是一个简单的示例代码,演示了 Fortran 调用 C++ 动态库的过程: **C++ 代码** ```cpp // example.cpp #include <iostream> extern "C" void hello() { std::cout << "Hello, C++!" << std::endl; } ``` **编译 C++ 代码成动态库** ```bash g++ -shared -fPIC example.cpp -o libexample.so ``` **Fortran 代码** ```fortran ! example.f90 module example implicit none interface subroutine hello() bind(C, name="hello") end subroutine hello end interface end module example program test use iso_c_binding use example implicit none interface subroutine c_hello() bind(C, name="hello") end subroutine c_hello end interface type(c_ptr) :: ptr procedure(c_hello), pointer :: c_func ! 将 C++ 函数的指针转换为 Fortran 中的指针 call c_f_pointer(c_func, ptr) ! 调用 C++ 函数 call c_func() end program test ``` 注意事项: 1. 在 Fortran 代码中声明 C++ 函数时,需要使用 `bind(C)` 关键字指定调用约定。C++ 函数的名称需要使用 `name` 属性指定。 2. 在 Fortran 代码中调用 C++ 函数时,需要将 C++ 函数的指针转换为 Fortran 中的指针,以便在 Fortran 中调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值