Fortran Coder

标题: generic interface无法区分c_float与real [打印本页]

作者: 愤怒的三炮    时间: 前天 21:35
标题: generic interface无法区分c_float与real
下面的代码对c_float接口和real接口的函数进行重载:
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode

program main

    use iso_c_binding
    implicit none
   
    interface print_number
        procedure:: print_number_real, print_number_c_float
    end interface
  
   
    call print_number(1.0)
   
   
    contains
   
        subroutine print_number_real(a)
            real:: a
            print*, a
        
        end subroutine
        
        subroutine print_number_c_float(a)
            real(c_float):: a
            print*, a
        
        end subroutine

end program


提示错误:Error: Ambiguous interfaces in generic interface 'print_number' for 'print_number_real' at (1) and 'print_number_c_float' at (2)

难道是编译器把c_float和real当成同一种类型了吗?

作者: 愤怒的三炮    时间: 前天 21:43
GCC version 14.1.0 和 intel fortran 2021.13.1 皆是如此。
作者: 愤怒的三炮    时间: 昨天 07:56
确实是这样,已解决。
作者: 楚香饭    时间: 昨天 08:40
本质上都是 float32,32位浮点。
重载看他本质上的类型,而不是看他取了个什么别名。
作者: 愤怒的三炮    时间: 昨天 09:00
楚香饭 发表于 2025-7-17 08:40
本质上都是 float32,32位浮点。
重载看他本质上的类型,而不是看他取了个什么别名。 ...

是的,c_float只是一个别名。
感谢刘涛解答~




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2