在同一ASSOCIATE构造中的另一个关联实体中引用一个关联名称 [英] Refering to an associating-name in another associating entity in the same ASSOCIATE construct
问题描述
我很抱歉,我的普通话不是很好,但是以下合法的Fortran 2008版本不是吗?
I appologize if my standardese isn't very good but Isn't the following legal Fortran 2008?
program test
implicit none
associate ( a => 6, b => 2*a )
print*, b
end associate
end program
我的编译器抱怨 a
没有声明并且没有隐式类型.
My compiler complains about a
not being declared and not having an implicit type.
我认为选择器只能是一个表达式或变量,而不能是一个关联名称或涉及一个的表达式.是这样吗?
I think the selector can only be an expression or variable and never be an associate-name or an expression involving one. Is this the case?
推荐答案
这是一个(嵌套的)版本,可以满足您的要求,并且您的版本不符合Fortran标准,
Here is a (nested) version that does what you wish, and your version which does not comply with the Fortran standard,
program test
implicit none
integer a ! note that a has be defined here because it is used as a VARIABLE in the association list below
associate ( a => 6, b => 2*a )
print*, b
end associate
associate ( a => 6 )
associate ( b => 2*a )
print*, b
end associate
end associate
end program
这是Gfortran编译的程序的输出,
Here is the output of the program compiled by Gfortran,
$gfortran -std=gnu main.f90 -o main
$main
483386368
12
根据Fortran标准, associate
构造允许人们将名称与变量或表达式的值相关联,在一个块的持续时间内.这是一般语法,
According to the Fortran standard, the associate
construct allows one to associate a name either with a variable or with the value of an expression, for the duration of a block. Here is the general syntax,
[ construct-name: ] associate ( association-list )
block
end associate [ construct-name ]
因此,我相信您对 associate
构造的使用不符合该标准.基本上,在您的代码中,编译器假定 b =>中的
表示已在关联构造和列表之外定义的变量 a
.关联列表中的2 * a a
(与名称 a
相反,在关联列表中定义).
Therefore, I believe your usage of the associate
construct does not comply with the standard. Basically, in your code, the compiler assumes that a
in b => 2*a
in the association list refers to a variable a
that has been already defined outside the associate construct and list (as opposed to the name a
that is defned in the association list).
就像@HighPerformanceMark所建议的那样,嵌套的 associate
构造(如上面的示例)可以实现您想要的目标.
As @HighPerformanceMark suggested, nested associate
constructs like the example above could achieve your desired goal.
这篇关于在同一ASSOCIATE构造中的另一个关联实体中引用一个关联名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!