如何在Fortran中进行几项枚举类型?

发布于 2025-01-30 00:27:31 字数 832 浏览 4 评论 0原文

我试图在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2025-02-06 00:27:31

Fortran在您希望使用的意义上没有枚举类型。 1

Fortran中的枚举是一组枚举者。问题的程序有两个。

枚举者本身是与C相应的枚举类型互操作的(整数)常数。它们是出于C互操作性的目的而存在,并且不提供Fortran内部的类似功能。

枚举者green问题中的大型是两个带有值3的整数常数(在某些,可能是不同的类型中)。 green ==大型无论常数的类似参数是真实的表达式。

Fortran中没有将变量限制为枚举值的机制。常数可以等效地将其声明为

integer(kind=enum_kind1) :: Green = 3_enum_kind1
integer(kind=enum_kind2) :: Large = 3_enum_kind2

适当的类值(在这种情况下很可能是相同的: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 and Large 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

integer(kind=enum_kind1) :: Green = 3_enum_kind1
integer(kind=enum_kind2) :: Large = 3_enum_kind2

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文