Hi all, the attached code does not compile: gfortran -c cst.f90 cst.f90:6.31: type(c_ptr), parameter :: p2 = pp 1 Error: non-constant initialization expression at (1) However, as far as I can tell, the code is correct (ifort accepts it). gfortran --version GNU Fortran (GCC) 4.9.0 20130813 (experimental) module m use iso_c_binding implicit none type(c_ptr), parameter :: pp = c_null_ptr ! this works type(c_ptr), parameter :: p2 = pp ! this doesn't work end module m
(In reply to mrestelli from comment #0) > > type(c_ptr), parameter :: p2 = pp > 1 > Error: non-constant initialization expression at (1) > > However, as far as I can tell, the code is correct (ifort accepts it). well, just because ifort accepts it doesn't mean that it's valid. In fact the corresponing case with actual Fortran pointers (i.e. pointer initialization) is only allowed since F08, cf. PR45290. One should check the Fortran standard for restrictions in the case of C_PTRs (which, strictly speaking, are no actual pointers in the Fortran sense).
(In reply to janus from comment #1) > One should check the > Fortran standard for restrictions in the case of C_PTRs (which, strictly > speaking, are no actual pointers in the Fortran sense). Since C_PTR is a derived type in the Fortran sense, I don't see why any restrictions on pointer init should apply. Unless there are any special restrictions for C_PTR, the code should be valid, just as the corresponding code with any other derived type: module m implicit none type :: t end type type(t), parameter :: pp = t() type(t), parameter :: p2 = pp end module m
In F03, I think the relevant quotes are: R506 initialization is = initialization-expr or => null-init 7.1.7 Initialization expression An initialization expression is an expression with limitations that make it suitable for use as a kind type parameter, initializer, or named constant. It is an expression in which each operation is intrinsic, and each primary is (1) A constant or subobject of a constant, ... Since pp is clearly a constant, the code should be valid (probably also in F08).
(In reply to janus from comment #1) > (In reply to mrestelli from comment #0) > > > > type(c_ptr), parameter :: p2 = pp > > 1 > > Error: non-constant initialization expression at (1) > > > > However, as far as I can tell, the code is correct (ifort accepts it). > > well, just because ifort accepts it doesn't mean that it's valid. Janus, yes, of course you are right, what I wanted to say was that the code seems fine according to my understanding of what "should happen", and for what is worth also ifort is happy with it. But I see that my sentence was not clear. Thank you for checking the relevant text in the standard, and also for the nice example type(t), parameter :: pp = suitable_initialization_expr_for_type_t type(t), parameter :: p2 = pp I would assume that, provided the first assignment is correct, the second one is always correct, whatever the definition of type(t). Am I right? Marco
(In reply to mrestelli from comment #4) > type(t), parameter :: pp = suitable_initialization_expr_for_type_t > type(t), parameter :: p2 = pp > > I would assume that, provided the first assignment is correct, the > second one is always correct, whatever the definition of type(t). Am I > right? I would think so. It is important, though, that pp is a PARAMETER.
Confirmed at r206083.