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

查看:218
本文介绍了将可分配字符传递给在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.

这个例程的摘录在这里:

An excerpt of this routine is here:

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之后,值为空(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.

当然我有一些替代品,但是能够使用单个rountine和没有可选参数的不同类型的数据的想法是非常好的。

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.

我可以使用用户定义

但我想知道这是否是Fortran 2008的默认行为。
并且,如果有一种方法来实现这一点,使用这个例程,用单个类(*)虚拟的争论,转换不同的类型,包括被引用的可分配字符。有一种方法可以强制在例程中进行分配,例如?

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?

我会感谢您的意见。
Cheers,
Eduardo

I'll appreciate your comments. Cheers, Eduardo

推荐答案

在选择类型strong>从不具有可分配属性(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 dummy参数未声明为可分配。您不能将未分配的实际参数与非可选的不可分配虚拟参数相关联。除此之外,您不能在select类型或关联构造中使用未分配的选择器。

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.

您需要分配实际参数在调用之前的某个长度,则关联名将在select类型构造内具有固定长度。或者,将可分配字符变量作为派生类型中的组件包装。

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天全站免登陆