- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I couldn't find information on whether it was standards-conforming to have an elemental function with a derived type result. Does anyone know? For example, the following code compiles with no warnings:
Command to compile: ifort /stand:f95 test_derel
[fortran]module mymod implicit none type mytype real(8) :: a integer :: i end type mytype contains elemental function identity(m1) result(m2) implicit none type(mytype), intent(in) :: m1 type(mytype) :: m2 m2 = m1 end function identity end module mymod program test_derel use mymod implicit none type(mytype), dimension(1:2) :: m1,m2 m1(1)%a = -1.d0 m1(2)%a = -2.d0 m1(1)%i = -1 m1(2)%i = -2 m2 = identity(m1) write(*,*) m2(1)%a,m2(1)%i write(*,*) m2(2)%a,m2(2)%i end program[/fortran]
And this code produces
-1.00000000000000 -1
-2.00000000000000 -2
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As far as I know, it is standard conforming. What led you to ask this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
From 12.7.1 of F2003 standard:
C1277 All dummy arguments of an elemental procedure shall be scalar dummy data objects and shall
not have the POINTER or ALLOCATABLE attribute.
C1278 The result variable of an elemental function shall be scalar and shall not have the POINTER orALLOCATABLE attribute.
There are no other constraints in the standard that the scalar be of intrinsic type.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Steve and Joseph.
Steve: For the economics problem I'm working, I have to keep track of a set of statistics that evolves over time. Today's set maps into tomorrow's set, and I wanted to program the mapping somewhat independently of the set of statistics (which may change).
Joseph: Thanks for the part of the standard. I wasn't sure whether a derived type of scalars was itself "scalar". Although I guess since Fortran handles complex scalars that would make sense.
This seems like a powerful feature: you can have an elemental function that in some sense returns an array! Although I am a little nervous using it...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Richard,
I asked because I didn't know why you thought this might be nonstandard.
The whole point of elemental functions is to be able to call them with either a scalar or an array and return the appropriate shape result.
I asked because I didn't know why you thought this might be nonstandard.
The whole point of elemental functions is to be able to call them with either a scalar or an array and return the appropriate shape result.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page