如何在Fortran中进行几项枚举类型?
我试图在Fortran中声明几种枚举类型。
这个有趣的简单示例很好地说明了我的问题:
program Main
enum, bind(c)
enumerator :: Colors = 0
enumerator :: Blue = 1
enumerator :: Red = 2
enumerator :: Green = 3
end enum
enum, bind(c)
enumerator :: Size = 0
enumerator :: Small = 1
enumerator :: Medium = 2
enumerator :: Large = 3
end enum
integer(kind(Colors)) :: myColor
myColor = Green
if (myColor == Large) then
write(*,*) 'MyColor is Large'
end if
end program Main
我还试图将这种枚举包裹在一种类型中,而其他许多东西都可以,但没有任何事情。
在这里,我可以将颜色
与size
进行比较。例如,在C中,当我声明color
和a size
typedef枚举时,我没有这样的问题,因为两种类型是不同的。
在Fortran中具有几种枚举类型是否存在简单的解决方案?
否则,我想像一个具有数字成员的几种类型,该整数成员持有值,然后创建接口以超载我需要的运算符(比较,情感等)。我不确定解决方案是否可能,而且我可以做到。
I tried to declare several enumeration types in Fortran.
This funny simple example illustrates well my problem :
program Main
enum, bind(c)
enumerator :: Colors = 0
enumerator :: Blue = 1
enumerator :: Red = 2
enumerator :: Green = 3
end enum
enum, bind(c)
enumerator :: Size = 0
enumerator :: Small = 1
enumerator :: Medium = 2
enumerator :: Large = 3
end enum
integer(kind(Colors)) :: myColor
myColor = Green
if (myColor == Large) then
write(*,*) 'MyColor is Large'
end if
end program Main
I also tried to enclose this enumeration in a type and many others things but none works.
Here I can compare Colors
with Size
. In C, for example, when I declare color
and a size
typedef enum, I have no such problem, because the two types are different.
Does it exist a simple solution to have several enumerated type in Fortran?
Otherwise, I imagine to declare several types with one integer member that holds the value and, after, to create interface to overload the operators I need (comparison, affectation and so on). I am not sure that solution is possible and also, I can do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Fortran在您希望使用的意义上没有枚举类型。 1
Fortran中的枚举是一组枚举者。问题的程序有两个。
枚举者本身是与C相应的枚举类型互操作的(整数)常数。它们是出于C互操作性的目的而存在,并且不提供Fortran内部的类似功能。
枚举者
green
和问题中的大型
是两个带有值3的整数常数(在某些,可能是不同的类型中)。green ==大型
无论常数的类似参数是真实的表达式。Fortran中没有将变量限制为枚举值的机制。常数可以等效地将其声明为
适当的类值(在这种情况下很可能是相同的:
c_int
),而Fortran程序将没有区别。如果您想在C和类似语言中存在枚举类型的意义,则必须使用非intrinsic方法(如问题所示)。
1 当前2018年的修订是这种情况。目前,有一个建议下一个修订版(临时2023年),将枚举类型包括在这里所需的内容。该规范在一个特定。
Fortran does not have enumerated types in the sense that you wish to use.1
An enumeration in Fortran is a set of enumerators. The program of the question has two of them.
Enumerators themselves are named (integer) constants of a kind interoperable with C's corresponding enumeration type. They exist for the purposes of C interoperability and not to provide a similar functionality within Fortran.
The enumerators
Green
andLarge
in the question are two named integer constants with value 3 (of some, possibly different kind).Green==Large
is a true expression whatever the kind parameters of the constants.There is no mechanism in Fortran to restrict a variable to values of an enumeration. The constants could equivalently be declared as
for the appropriate kind values (which are quite likely in this case to be the same:
C_INT
) and the Fortran program would know no difference.If you wish to use enumerated types in the sense that they exist in C and similar languages, you will have to use a non-intrinsic approach (as intimated in the question).
1 This is the case for the current, 2018, revision of the language. At this time, there is a proposal for the next revision (provisionally 2023) to include enumerated types closer to what is desired here. This specification is given in 7.6.2 of one particular working draft.