Fortran派生类型:重载赋值运算符不与“PARAMETER”属性一起使用 [英] Fortran derived types: Overloaded assignment operator not working with 'PARAMETER' attribute

查看:436
本文介绍了Fortran派生类型:重载赋值运算符不与“PARAMETER”属性一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用派生类型(bicomplex)和赋值运算符(=)的重载,以便可以将 real * 8 赋值给重复变数。 bicplx模块的MWE如下所示:

$ $ $



$ b COMPLEX * 16 :: a
COMPLEX * 16 :: b
结束类型

接口分配(=)
模块过程dble_assign_bicplx
结束接口

包含

子程序dble_assign_bicplx(qleft,xright)
隐式无
类型(bicomplex),intent(out):: qleft
双精度,意图(in):: xright

qleft%a = xright
qleft%b = 0.0d0
返回
结束子程序dble_assign_bicplx

结束MODULE

实数到复数的赋值由Fortran本质处理。这个方法在典型双变量变量上使用时效果很好,但是当我想将一个 real * 8 分配给具有 PARAMETER的双复本属性:

  TYPE(bicomplex):: test1 
test1 = 1.d0

完美无缺,但:

  TYPE(bicomplex),parameter :: test1 = 1.d0 

确实(1)和错误:无法将REAL(8)转换为以下错误:错误:PARAMETER中不兼容的派生类型TYPE(bicomplex)at(1)
然而,

  TYPE(bicomplex),parameter :: test1 = bicomplex(1.d0,0.d0) 

完美无缺。



有没有我缺少的东西,还是标准的一部分,通过使用重载赋值运算符无法分配参数?理想情况下,我想用F90 / F95(而不是2003或2008)来解决这个问题。 解决方案

是一个命名常量。语言的规则要求可以在编译时 - 即在程序执行之前确定一个命名常量的值。这意味着用于给定命名常量的表达式需要能够在编译时确定。最近标准中的语言术语是常量表达式,在以前的标准中它也被称为初始化表达式(在程序执行之前可以用作初始化程序)。

定义的赋值是一个可执行的动作 - 运行时程序执行位于已定义赋值后面的过程中的语句。这不是通常可以在编译时完成的事情。



正如您已经发现的那样,您可以使用结构构造函数,其中组件初始化器是常量表达式以初始化派生类型常量。


I am using a derived type (bicomplex), and an overload of the assignment operator (=), so that one can assign a real*8 to bicomplex. A MWE of the bicplx module follows:

MODULE bicplx

  type bicomplex
    COMPLEX*16 :: a
    COMPLEX*16 :: b
  end type

  interface assignment(=)
    module procedure dble_assign_bicplx
  end interface

contains

  subroutine dble_assign_bicplx(qleft, xright) 
    implicit none         
    type(bicomplex), intent(out) :: qleft
    double precision, intent(in) :: xright

    qleft%a = xright
    qleft%b = 0.0d0          
    return
  end subroutine dble_assign_bicplx

end MODULE

The assignment of real to complex is handled intrinsically by Fortran. This method works well when used on "typical" bicomplex variables, but breaks when I want to assign a real*8 to a bicomplex which has the PARAMETER attribute:

TYPE(bicomplex) :: test1
test1 = 1.d0

works perfectly, but:

TYPE(bicomplex), parameter :: test1 = 1.d0

does not compile and gives 2 errors: Error: Incompatible derived type in PARAMETER at (1), and Error: Can't convert REAL(8) to TYPE(bicomplex) at (1). However,

TYPE(bicomplex), parameter :: test1 = bicomplex(1.d0, 0.d0)

works perfectly.

Is there something I am missing, or is it part of the standards that a parameter can not be assigned by using an overloaded assignment operator? Ideally, I'd like to solve this using F90/F95 (and not 2003 or 2008).

解决方案

A "parameter" is a named constant. The rules of the language require that the value of a named constant can be determined at "compile time" - i.e. before the program is executed. That means that the expression used to give a named constant its value need to be able to be determined at compile time. The language terminology for this in recent standards is "constant expression", in earlier standards it was also called "initialization expression" (something that could be used as an initializer - before program execution).

Defined assignment is an executable action - when run the program executes the statements in the procedure that sits behind the defined assignment. This is not something that can generally be done at compile time.

As you have discovered, you can use structure constructors where the component initializers are constant expressions to initialize derived type named constants.

这篇关于Fortran派生类型:重载赋值运算符不与“PARAMETER”属性一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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