gpt4 book ai didi

pointers - 为什么 Fortran 指针总是可变的

转载 作者:行者123 更新时间:2023-12-01 16:11:53 25 4
gpt4 key购买 nike

我刚开始在 Fortran 中使用指针,最近偶然发现了这样一个事实,即 Fortran 中的指针不能保持不变。这意味着,这样的事情是不可能的:

procedure(proc_type), pointer, parameter :: fPtr => myFunc

我试图找出这不可能的原因。但我发现的唯一内容是在“现代 Fortran 解释”中:

Note also that a constant may not be a pointer [..] since these are always variables.

但这并不能解释背后的原因。有人知道吗?

最佳答案

因为 Fortran 规则是这样说的

Fortran 2008 (ISO/IEC 1539-1:2010):

5.3.14 POINTER attribute

1 Entities with the POINTER attribute can be associated with different data objects or procedures during execution of a program. A pointer is either a data pointer or a procedure pointer. Procedure pointers are described in 12.4.3.6.

这与常量不兼容。

为什么会有这样的规则?因为标准的作者这样做了。

他们为什么那样做?这个问题的答案通常很简单——因为没有人提出不同的规则供委员会讨论和批准,或者委员会的一些成员不喜欢它。你真的要问他们 - J3SC22/WG5 ,但要准备好回答没有具体原因。

在某些属于同一类别的语言中,例如C 和 C++,常量指针是可能的。一个常量指针初始化为指向一个整型常量

const int i = 3;
static int* const x=(int*)&i;

初始化为指向整数函数的常量指针

int fun(){
return 1;
}

static int (* const x)()=&fun;

在 Fortran 中绝对可以允许这样的事情。请联系您在 Fortran 标准委员会中的代表,以提出此​​类功能的建议。

如 IanH 和 credondo 所示,有一些方法可以规避此限制,但此答案试图与原始问题保持一致。 为什么会有这个限制?

关于pointers - 为什么 Fortran 指针总是可变的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26605098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com