在Fortran中没有解除对数组的标量参数 [英] No lifting of scalar arguments to arrays in Fortran
问题描述
x = foo(7)在这种情况下,错误消息可能是另一种方法吗? )
,会产生GFortran错误:错误:参数'a'在(1)(1和0)
中的排名不匹配。 模块m
public :: foo
包含
函数foo(a)结果(b)
整数,维(:) :: a
整数,维(大小(a)):: b
b = a + 1
结束函数foo
结束模块m
程序p
使用m
整数,维数(4): :x
整数,参数,维数(4):: y =(/ 1,2,3,4 /)
x = 7
x = foo(x)
x = foo(y)
x = foo(x + 7)
x = foo(7)
结束程序p
这个问题应该问到为什么数组赋值会将标量值源提升为数组目标;不像数组函数。我期望这只是一个方便的特例。任何评论感激地收到在下面的乞讨帽。
如果您希望函数处理缩放器和数组参数,请将其声明为元素和缩放器伪参数。然后它将能够处理缩放器和数组实际参数,包括缩放器表达式。满足你的需求吗? 也许他们提供了一种做你想做的事的方法,一种方法就够了吗? Why is it that Fortran will promote a scalar expression to an array, in an expression, but not as an argument to a procedure? In particular, why did the standards body make this design decision? Is it solely because of ambiguity, should the procedure be overloaded? Could an error message in that situation be an alternative approach? For example, In the code below, the last statement, This question should have asked about why an array assignment will promote a scalar value source to an array target; unlike an array function. I expect that's simply a convenient special case though. Any comments gratefully received in the begging caps below. If you want the function to handle scaler and array arguments, declare it as "elemental" and with scaler dummy arguments. Then it will be able to handle both scaler and array actual arguments, including scaler expressions. Will that meet your need? The change: Perhaps they provided a way to do what you want, and one way was enough? 这篇关于在Fortran中没有解除对数组的标量参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p
$ b $ pre $元元素函数元素函数foo(a)结果(b)
整数,意图(in):: a
整数:: b
b = a + 1
结束函数foo
x = foo(7)
, produces the GFortran error: Error: Rank mismatch in argument 'a' at (1) (1 and 0)
.module m
public :: foo
contains
function foo(a) result(b)
integer, dimension(:) :: a
integer, dimension(size(a)) :: b
b = a+1
end function foo
end module m
program p
use m
integer, dimension(4) :: x
integer, parameter, dimension(4) :: y = (/1,2,3,4/)
x = 7
x = foo(x)
x = foo(y)
x = foo(x + 7)
x = foo(7)
end program p
elemental function foo(a) result(b)
integer, intent (in) :: a
integer :: b
b = a+1
end function foo