Quantcast
Viewing all articles
Browse latest Browse all 415

Error in overloading constructors of parametric types.

When compiling with ifort 2017 update 1:

#     ifndef matrix_algebra_F90
#     define matrix_algebra_F90

      module matrix_algebra

            implicit none

            integer,parameter::x_=8
            type::vector(size)
               integer,len::size
               complex(kind=x_)::component(0:size)
            contains
               procedure,private::initialize_vector
        end type  vector

            interface vector
               module procedure vector_constructor
        end interface vector

      contains

            subroutine initialize_vector(this,source)

                  implicit none

                  class(vector(*))::this
                  complex(kind=x_),intent(in),optional::source(0:this%size)

                  if(present(source)) then
                     this%component=source
                  else
                     this%component=(0.0_x_,0.0_x_)
              end if!present(source)

        end subroutine initialize_vector!this,source

            function vector_constructor(size,source) result(that)

                  implicit none

                  integer,intent(in)::size
                  complex(kind=x_),intent(in),optional::source(0:size)
                  type(vector(size))::that

                  if(present(source)) then
                     call that%initialize_vector(source)
                  else
                     call that%initialize_vector()
              end if!present(source)

        end function vector_constructor!size,source

            function test_constructor(vector0) result(vector_)

                  implicit none

                  type(vector(*)),intent(in)::vector0
                  type(vector(vector0%size))::vector_

                  vector_=vector(vector0%size,(0.0_x_,0.0_x_))

        end function test_constructor

  end module matrix_algebra

#     endif

I get the following error:

matrix/snippet.F90(97): error #8725: If a type parameter does not have a default value, there must be a type parameter spec corresponding to that type parameter.   [VECTOR]
                  vector_=vector(vector0%size,(0.0_x_,0.0_x_))
^
matrix/snippet.F90(97): error #6593: The number of expressions in a structure constructor differs from the number of components of the derived type.   [VECTOR]
                  vector_=vector(vector0%size,(0.0_x_,0.0_x_))
--------------------------^

This disallows me from defining custom constructors, something I was aware of being able to do. I suspect that the class name is already bound by the compiler as a synthesized constructor that simply is called to at least initialize all members with no in-class initializers (default values). In that case, the compiler prevents me from creating additional constructors to the default ones (overloading the constructor). Could you please tell me what detail I am missing here? Thank you in advance for your time.

EDIT: When using constructors in the aforementioned manner in more complex code with more types and constructors, it generates a internal compiler error, but it is certain that the constructors are the culprits, as removing all constructor calls resolves the internal error. In an attempt to make the above snippet to reproduce the problem I get a normal compiler error instead, but I think it is related still.


Viewing all articles
Browse latest Browse all 415

Trending Articles