Fortran-derived constructor type defined using function C (II)

The following question is a constructor of a Fortran type constructor defined using the C function , I approached this inoperative example with gfortran 4.9:

module my_module
  use iso_c_binding
  type, bind(c) :: my_double
     real(c_double) :: x
     real(c_double) :: y
  end type my_double
  interface my_double
      procedure my_module_fortran_new_my_double
  end interface
  interface
     type(my_double) function my_module_fortran_new_my_double (v) bind ( c )
       use iso_c_binding
       import :: my_double
       real (c_double), intent(in) :: v
     end function my_module_fortran_new_my_double
  end interface
end module my_module

program main
  use my_module
  type(my_double) x
  x = my_double(12)
end program main

Following the previous question , a Fortran-derived type constructor defined using the C function , fixing the module works fine. However, the specified constructor is not recognized by my compiler.

Here is the compiler output:

$ gfortran -std=f2008 test.f90 -o test.o -c
test.f90:22.6:

  x = my_double(12)
      1
Error: No initializer for component 'y' given in the structure constructor at (1)!

It looks like my used constructor is not taken into account. Can someone help me understand what I did wrong (again)?

+4
source share
1 answer

,

my_double(12)

type(my_double) function something(i, ...) ...
   integer ... :: i
   ..., optional, ... :: ...
end function

integer.

, , , real(c_double).

, :

  • , ;
  • , .

:

x = my_double(12._c_double)   ! c_double from intrinsic iso_c_binding

, .

x = my_double(12,13)  ! Using the normal constructor
x = my_double(12)     ! Using the normal constructor if default
                      ! initialization applies for component y

, ( ) / . .

+3

Source: https://habr.com/ru/post/1664526/


All Articles