什么是假定的长度字符函数结果,为什么要避免它? [英] What is an assumed length character function result, and why should it be avoided?

查看:157
本文介绍了什么是假定的长度字符函数结果,为什么要避免它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对关于非恒定长度的字符函数结果的问题,答案中提到了假定的长度函数结果,但没有详细说明。



如果我想返回一个字符结果,而长度依赖于其他字符,那么这个回答就会提到自动对象和延迟长度作为可能的方法:

 函数deferred_length(x)
字符(*),intent(in):: x
字符(:),allocatable :: deferred_length
deferred_length = x //'!'
结束函数

 函数automatic(x)
字符(*),intent(in):: x
字符(LEN(x)+1):: automatic
automatic = x //'!'
结束函数

什么是假设的长度函数结果,它与上面的表格有什么不同?此外,为什么这样提到一个注脚?

解决方案

假设长度字符函数如下所示:


$ b $

 函数assume_len(x)
字符(*),intent(in):: x
字符(*): :assume_len
assume_len = x //'!'
结束函数

假参数 x 和函数结果 assume_len 都是假定的长度字符。



就像 x 在函数被引用时从实际参数中假定它的长度一样,函数结果假定它的长度来自函数声明 in

使用上述函数,请考虑程序

  implicit none 
character(len = 11):: x ='Hello world'
character(len = 12)assume_len

print *,assume_len(x )

结束

在函数执行过程中,函数结果的长度为12,如主程序中声明的那样。同样,如果我们希望它的长度为5,我们只需将主程序中的声明更改为字符(len = 5)assume_len 。在一个不同的程序单元中,声明可能会有所不同,函数结果会在被引用时假定长度。



好吧,这看起来不那么有害:我们为什么要避免使用类似这样的函数?


  • 它们违背了现代Fortran的哲学;

  • 在现代Fortran中有更好的方法来完成同样的事情;

  • 它们非常有限。


<在Fortran的其他方面,函数结果的属性仅取决于函数的参数和其他可访问的信息。在这里,函数结果的属性可能与其他所有内容的声明不同。



其他要点:


  • 这个函数的接口必须隐含在引用的地方(我们不喜欢隐式接口);
  • 我们不能引用同一个函数在同一个地方重复两次,并得到不同的长度结果,甚至是不同的输入;
  • 该函数对返回的长度有了更好的理解。



在那些我们确实想要控制引用位置的长度的情况下,我们可以替换 assume_len ($)
$ b pre $ 函数替换(x,n)
字符(*),intent(in):: x
整数,意图(in):: n
字符(n)::替换
...
结束函数

或子程序

 子程序替换(x,y )
字符(*),意图(in):: x
字符(*),意图(输出):: y
...
结束子程序
$ b $ <$ p
$ b $ p>

b

In response to a question about character function results of non-constant length, an answer mentions "assumed length function result" but doesn't go into detail.

If I want to return a character result where the length depends on something else, that answer mentions automatic objects and deferred length as possible approaches:

function deferred_length(x)
  character(*), intent(in)  :: x
  character(:), allocatable :: deferred_length
  deferred_length = x//'!'
end function

or

function automatic(x)
  character(*), intent(in)  :: x
  character(LEN(x)+1)       :: automatic
  automatic = x//'!'
end function

What is an assumed length function result and how does it differ from the forms above? Further, why is such mention relegated to a footnote?

解决方案

An assumed length character function looks like the following:

function assumed_len(x)
  character(*), intent(in)  :: x
  character(*)              :: assumed_len
  assumed_len = x//'!'
end function

Here the dummy argument x and the function result assumed_len are both assumed length characters.

Much like how x assumes its length from the actual argument when the function is referenced, the function result assumes its length from the function declaration in the referencing place.

With the above function, consider the program

implicit none
character(len=11) :: x='Hello world'
character(len=12) assumed_len

print *, assumed_len(x)

end

During the function's execution the function result has length 12 as declared in the main program. Equally, if we wanted it to have length 5 we simply change the declaration in the main program to character(len=5) assumed_len. In a different program unit the declaration could be something different and the function result would assume that length when referenced.

Well, that doesn't look so harmful: why should we avoid using functions like this?

  • They go against the "philosophy" of modern Fortran;
  • There are better ways in modern Fortran to accomplish the same thing;
  • They are quite limited.

In every other aspect of Fortran, the attributes of a function result depends only on the arguments of the function and other accessible information. Here the attributes of the function result may differ with everything else the same through a declaration.

To the other points:

  • the interface of such a function must be implicit in a referencing place (and we don't like implicit interfaces);
  • we can't reference the same function twice in one place and get different length results, even with differing input;
  • the function has a much better idea of what length it wants to return.

In those cases where we really do want to control the length in the referencing place we can replace assumed_len with a function like

function replacement(x, n)
  character(*), intent(in) :: x
  integer, intent(in)      :: n
  character(n)             :: replacement
  ...
end function

or a subroutine

subroutine replacement(x, y)
  character(*), intent(in)  :: x
  character(*), intent(out) :: y
  ...
end subroutine

Finally, assumed length character functions are declared obsolescent in the current Fortran standard, and may be deleted in later revisions.

这篇关于什么是假定的长度字符函数结果,为什么要避免它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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