在 Fortran 中将可分配字符传递给具有无限多态虚拟参数的子例程 [英] Passing an allocatable character to a subroutine with unlimited polymorphic dummy argument in Fortran

查看:36
本文介绍了在 Fortran 中将可分配字符传递给具有无限多态虚拟参数的子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个例程,该例程能够基于无限多态性将字符串转换为不同类型的数据类型.这个想法是用户调用这个例程,将变量传递到它想要存储数据的位置,以及基于变量/参数类型定义转换的例程.

I'm trying to write a routine that is able to convert a string into different kinds of data type, based on unlimited polymorphism. The idea is the user call this routine, passing the variable where it wants to store the data, and the routine to define the conversion based on the variable/argument type.

此例程的摘录如下:

subroutine GetAsScalar (this, value, status)
    !Arguments-------------------------------------------------------------
    class(TKeyword)                                 ::  this
    class(*)                                        ::  value
    logical, optional                               ::  status

    !Local-----------------------------------------------------------------
    integer                                         ::  stat        

    !----------------------------------------------------------------------  

    stat = 0

    select type (value)
    type is (REAL(real32))      !single precision
        read (this%fValue, *, IOSTAT = stat) value           
    type is (REAL(real64))      !double precision
        read (this%fValue, *, IOSTAT = stat) value
    type is (LOGICAL)
        read (this%fValue, *, IOSTAT = stat) value
    type is (INTEGER(int32))    !integer
        read (this%fValue, *, IOSTAT = stat) value
    type is (INTEGER(int64))    !long integer
        read (this%fValue, *, IOSTAT = stat) value
    type is (CHARACTER(*))
        value = this%fValue
    class default            
        this%Message = "Invalid data type"
        status = .false.
        return
    end select

    if (present (status)) then
        if (stat /= 0) then
            status = .false.
        else                    
            status = .true.
        endif
    endif

end subroutine GetAsScalar

this%fValue"是一个字符(len=:),可分配"字符串.当我尝试使用此例程传递可分配字符串时,它成功退出,没有错误/异常引发:

"this%fValue" is a "character(len=:), allocatable" string. When I try to use this routine passing an allocatable string, it exit with success, no error/exceptions raise:

character(len=:), allocatable :: value
call keyword%GetAsScalar(value)

但字符串value"始终为空.即使在例程内部,在赋值value = this%fValue"之后,value 也是空的(len(value) 为 0).

But the string "value" is always empty. Even inside the routine, after the assign "value = this%fValue", value is empty (len(value) is 0).

似乎编译器无法检测到参数是字符类型(len=:)、可分配的,因此无法为其赋值.

It seems that the compiler is unable to detected that the argument is of type character(len=:), allocatable, and so, is unable to assign the value to it.

当然,我有一些替代方案,但是能够使用单个例程并且没有可选参数来处理不同类型的数据的想法非常好.

Of course I have some alternatives, but the idea of being able to use a single rountine and without optional arguments for different kind of data is really nice.

例如,我可以使用我创建的用户定义类型来处理字符串.

I can use a user defined type that I create to handle strings, for example.

但我想知道这是否是 Fortran 2008 中的默认行为.而且,如果有办法做到这一点,使用这个例程和一个class(*)"虚拟参数,来转换不同的类型,包括引用的可分配字符.例如,有一种方法可以在例程内部强制分配?

But I would like to know if this is the default behaviour in Fortran 2008. And also, if there is a way to accomplish this, using this routine, with a single "class(*)" dummy argumment, to convert different types including the referred allocatable character. There is a way to force the allocation inside the routine, for example?

感谢您的评论.干杯,爱德华多

I'll appreciate your comments. Cheers, Eduardo

推荐答案

在选择类型(或关联)构造中,关联名称从不具有可分配属性(16.5.1.6p2),无论选择是否具有该属性.

In a select type (or associate) construct, the associate name never has the allocatable attribute (16.5.1.6p2), regardless of whether the select has that attribute.

在您的情况下,选择器也缺少该属性 - value 虚拟参数未声明为可分配的.不允许将未分配的实际参数与非可选的不可分配的虚拟参数相关联.除此之外,不允许在选择类型或关联构造中使用未分配的选择器.

In your case, the selector is also lacking that attribute - the value dummy argument is not declared to be allocatable. You are not permitted to associate an unallocated actual argument with a non-optional non-allocatable dummy argument. Beyond that, you are not permitted to use an unallocated selector in a select type or associate construct.

您需要在调用之前为 value 实际参数分配一定长度,然后 value 关联名称将在选择类型构造中具有该固定长度.或者,将可分配字符变量包装为派生类型中的组件.

You need to allocate the value actual argument to some length before the call, the value associate name will then have that fixed length inside the select type construct. Alternatively, wrap the allocatable character variable as a component in a derived type.

这篇关于在 Fortran 中将可分配字符传递给具有无限多态虚拟参数的子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆