Maybe the problem is in trying to write C++ in any other language?
You can have a read-only look at private values through a callback that implements whatever you need:
module basemod
use ISO_FORTRAN_ENV, RWP => REAL64
implicit none
private
public :: RWP
type, public :: mytype_t
private
real(RWP), allocatable :: myarray(:)
contains
procedure :: access_privates
end type
interface mytype_t
module procedure mytype_t_new
end interface
abstract interface
subroutine i_privates_cb(myarray)
import
real(RWP), intent(in) :: myarray(:)
end subroutine
end interface
public :: i_privates_cb
contains
pure function mytype_t_new(array) result(new)
type(mytype_t) :: new
real(RWP), intent(in) :: array(:)
new%myarray = array
end function
subroutine access_privates(self, cb)
class(mytype_t), intent(in) :: self
procedure(i_privates_cb) :: cb
if (.not. allocated(self%myarray)) then
call cb([real(RWP) ::])
return
endif
call cb(self%myarray)
end subroutine
end module basemod
module myimpl
use basemod
implicit none
private
public :: process
contains
subroutine process()
type(mytype_t) :: t
call t%access_privates(cb)
t = mytype_t([real(RWP) :: (i, integer :: i = 1, 100)])
call t%access_privates(cb)
contains
subroutine cb(array)
real(RWP), intent(in) :: array(:)
if (size(array) == 0) then
print'(/a)','there''s no data'
return
endif
print'(/a)','array is:'
print'(10(f8.4,:,1x))',array
end subroutine
end subroutine
end module myimpl
use myimpl
implicit none
call process()
end
My gfortran version still doesn’t like the declaration of i within the implied-do, but ifx is fine with it:
$ ifx readonly-access.f90 && ./a.out
there's no data
array is:
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000
11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000
21.0000 22.0000 23.0000 24.0000 25.0000 26.0000 27.0000 28.0000 29.0000 30.0000
31.0000 32.0000 33.0000 34.0000 35.0000 36.0000 37.0000 38.0000 39.0000 40.0000
41.0000 42.0000 43.0000 44.0000 45.0000 46.0000 47.0000 48.0000 49.0000 50.0000
51.0000 52.0000 53.0000 54.0000 55.0000 56.0000 57.0000 58.0000 59.0000 60.0000
61.0000 62.0000 63.0000 64.0000 65.0000 66.0000 67.0000 68.0000 69.0000 70.0000
71.0000 72.0000 73.0000 74.0000 75.0000 76.0000 77.0000 78.0000 79.0000 80.0000
81.0000 82.0000 83.0000 84.0000 85.0000 86.0000 87.0000 88.0000 89.0000 90.0000
91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000
And again, no pointers were harmed during the process,
.