Have a function in fortran return a reference that can be placed on the left-hand-side of an assignment

独自空忆成欢 提交于 2019-12-05 20:15:48

Let's look at what you want to do:

get(2)=5

and the error message

Error: 'get' at (1) is not a variable

That looks pretty comprehensive: you can't do what you want. Or, perhaps...

get(2) is indeed, under the rules of Fortran 2003, not a variable. In Fortran 2003 a variable is given by the rules R601 and R603, which is a list of designators.

The left-hand side of an assignment must be a variable.

But look at Fortran 2008 and its definition of a variable. Now a variable is either one of those same designators (or ones related to coarrays or complex parts), but it could also (C602 to R602) be a function reference which

shall have a data pointer result.

This is summarized in the introduction of Fortran 2008, detailing the extensions over Fortran 2003, as

A pointer function reference can denote a variable in any variable definition context.

get(2) is a reference to a function that has a data pointer result. get(2) then may appear on the left-hand side of an assignment statement under the rules of Fortran 2008.

Alas, this interpretation of Fortran is not widely supported by current compilers: at the time of answering just the Cray compiler.

This means that this answer is really saying that you have two options: switch compiler or wait until this feature is more widespread. As both of these are likely impractical, you probably want another answer which gives something slightly more portable as a workaround.

I prefer my link to that given by innoSPG, as although this latter is based on the former, the description of the appropriate field "Pointer functions - pointer function ref is a variable" is slightly more clear. This is, though, a more accessible document and a viable alternative.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!