是否可以使全局可用的用户定义的KIND参数可用? [英] Is it possible to make user-defined KIND parameters globally available?
问题描述
模块编号
整数,参数:: sing_prec = selected_real_kind(6)
integer,parameter :: doub_prec = selected_real_kind(15)
结束模块
每当我想在我的代码中使用类型 real(sing_prec):: foo
或类似地方的变量,我需要插入使用数字
在相应的程序单元中。是否有可能以全球的方式做到这一点,即使KIND全球定义可用?
这是否有任何影响?如果这些类型在许多地方都是必需的,那么在各处插入使用语句会使代码臃肿。类似于编译器标志,比如gfortran中的 -fimplicit none
,在任何地方强加隐式none
都不错,但我是无法找到这样的标志。
这个问题对于特定的Fortran版本来说并不是特定的。
在标准的Fortran中 - 没有。
在编译器特定的Fortran实现中 - 并没有我所知道的广泛的当前作物使用编译器。所有(?)都可以选择将默认实数(整数,复合数)视为4-或8字节,但不能选择性地将一些视为4-,有些则视为8-(或者您希望使用的任何长度)。
你可以破解它我猜 - 也许把所有代码放在一个范围内?
Arrrgggh 你恐惧地退缩,所有的代码在一个范围内!!!
=>最近的编程语言和系统倾向于增加模块化,并认为将一个范围的定义从一个范围导入到另一个范围是一件好事情 。 Fortran使用 use
,但其他语言中的其他语言中的其他类似机制则使用 import
, include 和类似的。
顺便说一句,您确实知道内部模块 iso_fortran_env
不是吗?你可能会重写你的代码片段。
use iso_fortran_env,sing_prec => real32,doub_prec => real64
Consider the following code:
module numbers
integer, parameter :: sing_prec = selected_real_kind(6)
integer, parameter :: doub_prec = selected_real_kind(15)
end module
Whenever I want to use variables of type real(sing_prec) :: foo
or similar somewhere within my code, I need to insert use numbers
in the respective program unit. Is it possible to do that in a "global" fashion, i.e. make the KIND definitions globally available?
Are there any implications to this? If the types are required in many places, inserting use statements everywhere makes the code quited bloated. Something similiar to compiler flags such as -fimplicit none
in gfortran for imposing implicit none
everywhere would be great, but I was not able to find such a flag.
This question is not peculiar to a specific Fortran version.
In standard Fortran - No.
In compiler specific implementations of Fortran - Not as far as I am aware with the current crop of widely used compilers. All (?) have options to treat default reals (integers, complexes, too) as either 4- or 8-byte, but not to selectively treat some as 4- and some as 8- (or whatever lengths you wish to make use of).
You could hack it I guess -- maybe put all code in one scope ?
Arrrgggh you recoil in horror, all the code in one scope !!!
which kind of takes me to this point => Most recent programming languages and systems have tended towards increasing modularisation and think it a good thing to explicitly import definitions from one scope to another. Fortran uses use
but other similar mechanisms in other languages go under the labels of import
, include
, and similar.
By the way, you do know about the intrinsic module iso_fortran_env
don't you ? You might rewrite your snippet along the lines
use iso_fortran_env, sing_prec => real32, doub_prec => real64
这篇关于是否可以使全局可用的用户定义的KIND参数可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!