抽象型によってサポートされるポリモーフィズムを使用した数値最適化のためのオブジェクト指向 Fortran コードを開発しています。これは TDD の優れた実践であるため、抽象型 ですべての最適化テストを記述しようとしていclass(generic_optimizer)
ますtype(newton_raphson)
。
すべての最適化テストは への呼び出しを特徴としてcall my_problem%solve(...)
おり、これは抽象型として定義されdeferred
ており、もちろん派生型ごとに異なる実装を特徴としています。
問題は、各非抽象クラスで遅延関数を として定義するとnon_overridable
、次のようなセグメンテーション違反が発生することです。
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0 0x0000000000000000 in ?? ()
#1 0x0000000000913efe in __newton_raphson_MOD_nr_solve ()
#2 0x00000000008cfafa in MAIN__ ()
#3 0x00000000008cfb2b in main ()
#4 0x0000003a3c81ed5d in __libc_start_main () from /lib64/libc.so.6
#5 0x00000000004048f9 in _start ()
non_overridable
試行錯誤の末、宣言を削除するとエラーを回避できることに気付きました。この場合、それは問題ではありませんが、このコードでは 2 つのレベルのポリモーフィズムが発生する可能性が低いため、強制したかったのです。代わりに、標準の要件に違反していましたか?
エラーを再現するサンプルコードを次に示します。gfortran 5.3.0 および 6.1.0 でテストしています。
module generic_type_module
implicit none
private
type, abstract, public :: generic_type
real(8) :: some_data
contains
procedure (sqrt_interface), deferred :: square_root
procedure, non_overridable :: sqrt_test
end type generic_type
abstract interface
real(8) function sqrt_interface(this,x) result(sqrtx)
import generic_type
class(generic_type), intent(in) :: this
real(8), intent(in) :: x
end function sqrt_interface
end interface
contains
subroutine sqrt_test(this,x)
class(generic_type), intent(in) :: this
real(8), intent(in) :: x
print *, 'sqrt(',x,') = ',this%square_root(x)
end subroutine sqrt_test
end module generic_type_module
module actual_types_module
use generic_type_module
implicit none
private
type, public, extends(generic_type) :: crashing
real(8) :: other_data
contains
procedure, non_overridable :: square_root => crashing_square_root
end type crashing
type, public, extends(generic_type) :: working
real(8) :: other_data
contains
procedure :: square_root => working_square_root
end type working
contains
real(8) function crashing_square_root(this,x) result(sqrtx)
class(crashing), intent(in) :: this
real(8), intent(in) :: x
sqrtx = sqrt(x)
end function crashing_square_root
real(8) function working_square_root(this,x) result(sqrtx)
class(working), intent(in) :: this
real(8), intent(in) :: x
sqrtx = sqrt(x)
end function working_square_root
end module actual_types_module
program deferred_test
use actual_types_module
implicit none
type(crashing) :: crashes
type(working) :: works
call works%sqrt_test(2.0_8)
call crashes%sqrt_test(2.0_8)
end program