访问运算符"[[],(),{}"在Fortran 90或2003中超载 [英] access operators "[ ], ( ), { }" overloading in Fortran 90 or 2003

查看:84
本文介绍了访问运算符"[[],(),{}"在Fortran 90或2003中超载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以重载FORTRAN 2003中派生数据类型的条目访问运算符[],()或{}吗?在下面的示例中,我想为派生数据类型"custom"定义访问方案.

Can I overload entry access operators [], () or {} for derived data types in FORTRAN 2003? In the following example, I want to define access scheme for the derived data type "custom".

type custom
   integer, dimension(:), allocatable :: a
end type custom

type(custom) :: t

! after some initialization 
! .....
! .....
! .....
!
t%a( (/ 1, 2, 5 /) ) ! return entries located in positions 1, 2, and 5
t{ (/ 1, 2, 5 /) }   ! ?????? I want to define what stuff it should return 

我该怎么做?

更新:

请注意,我不想直接使用数组"t%a"并对其进行常规的子数组操作.相反,我想为数据类型"custom"重新定义数组操作,以便t {'first'}应该在t%a或t%a(1)的第一个条目中返回一个指针,所以我可以说

Note that I don't want to use the array "t%a" directly and do conventional sub-array operation on that. Instead, I want to redefine array operation for data type "custom" such that t{'first'} should return a pointer the first entry in t%a or t%a(1) so I can say

t['first']= 18 

print *, t['first']. 

还有额外的重载,我想获得类似t [1] = 18的功能,就像t ['first'] = 18一样.

Also with additional overloading I want to get a functionality like t[1] = 18 works like t['first'] = 18.

推荐答案

这取决于您所说的返回".

This rather depends on what you mean by "return".

通过示例本身提供

t%a([1,2,5])   ! Using syntax offered by Fortran 2003

没有返回任何东西:它是一个子对象.通过引用该子对象,我们可以执行各种操作:

doesn't return anything: it's a subobject. With a reference to that subobject we can do various things:

print *, t%a([1,2,5])
t%a([1,2,5]) = 27
t%a([1,2,5]) = sin(real(t%a([1,2,5])))

但是仍然没有返回"的概念.至关重要的是,正如我们将要看到的那样,这些不是表达式.

but there's still no concept of "returning". Crucially, as we shall see, these are not expressions.

提到这个问题,t[]t()t{}可以表示什么意思,那么答案就是简单的否". * 例如,您可能想要说:

Coming to the question, can t[], t(), t{} mean something, then the answer is, simply, "no".* You may want, for example, to say:

t[1,2,5] = 1

表示

t%a[1,2,5] = 1

但这不是要考虑的事情.

but that is not something to consider.

可以创建类似

print *, t%ref([1,2,5])

但是我们处在无法定义的范围内.

but we're quite in the non-definable territory.

但是,正如您现在提到的指针一样,还有更多要说的.尽管首选语法t[1]t["first"]不可用,我们仍然可以选择类型绑定过程.例如,函数调用t%ref("first")可能能够返回指向t%a的第一个元素的指针.例如,t%ref(1)可能类似于

However, as you now mention pointers, there's more to say. Whilst the preferred syntax t[1] or t["first"] is not available we still have the option of type-bound procedures. For example, a function call t%ref("first") may well be able to return a pointer to the first element of t%a. For example, t%ref(1) could be like

module reference

  implicit none

  type custom
     integer, dimension(:), allocatable :: a
   contains
     procedure ref
  end type custom

contains

  function ref(t, idx)
    class(custom), target, intent(in) :: t
    integer, intent(in) :: idx
    integer, pointer :: ref

    ref => t%a(idx)
  end function ref

end module reference

  use reference
  implicit none

  type(custom), target :: t
  integer, pointer :: b

  t%a = [1, 2, 3, 4, 5]

  print *, t%a
  b => t%ref(1)  ! Fortran 2008 allows direct assignment
  b = 8          ! but compiler support is very limited.
  print *, t%a

end

如果需要,可以将ref设为通用,以便t%ref("first")(等)可以接受.

If desired ref can be made generic so that t%ref("first") (etc.) is acceptable.

* 我基于t是标量的事实.但是,正如弗拉基米尔F在评论()[]中提到的那样,可能确实意味着事情.第一个涉及数组,第二个涉及共同数组.语法是一个问题,但是这个答案更多地关注的是机制而不是语法.

* I'm basing that on the fact that here t is a scalar. However, as mentioned by Vladimir F in a comment () and [] potentially do mean things. The first relates to arrays and the second to co-arrays. Syntax, then, is an issue, but this answer looks more at the mechanism than syntax.

这篇关于访问运算符"[[],(),{}"在Fortran 90或2003中超载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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