(void)var;对于类似于C/C++ 中的技巧的特定变量,是否有任何可移植的方法可以在 Fortran 中抑制“未使用的虚拟参数”警告?
一个激励性的例子(应弗拉基米尔·F 的要求)。策略模式,GoF示例,具有不同的断行策略,省略不必要的细节。
module linebreaking
type, abstract :: linebreaking_compositor
contains
procedure(linebreaking_compositor_compose), deferred, pass(this) :: compose
end type
abstract interface
subroutine linebreaking_compositor_compose(this)
import linebreaking_compositor
class(linebreaking_compositor), intent(in) :: this
end subroutine linebreaking_compositor_compose
end interface
type, extends(linebreaking_compositor) :: linebreaking_simple_compositor
contains
procedure, pass(this) :: compose => linebreaking_simple_compositor_compose
end type linebreaking_simple_compositor
type, extends(linebreaking_compositor) :: linebreaking_tex_compositor
contains
procedure, pass(this) :: compose => linebreaking_tex_compositor_compose
end type linebreaking_tex_compositor
type, extends(linebreaking_compositor) :: linebreaking_array_compositor
private
integer :: interval
contains
procedure, pass(this) :: compose => linebreaking_array_compositor_compose
end type linebreaking_array_compositor
contains
subroutine linebreaking_simple_compositor_compose(this)
class(linebreaking_simple_compositor), intent(in) :: this
print *, "Composing using a simple compositor."
end subroutine linebreaking_simple_compositor_compose
subroutine linebreaking_tex_compositor_compose(this)
class(linebreaking_tex_compositor), intent(in) :: this
print *, "Composing using a TeX compositor."
end subroutine linebreaking_tex_compositor_compose
subroutine linebreaking_array_compositor_compose(this)
class(linebreaking_array_compositor), intent(in) :: this
print *, "Composing using an array compositor with interval", this%interval, "."
end subroutine linebreaking_array_compositor_compose
end module linebreaking
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,在方法中this需要传递对象虚拟参数,但在其他两个合成器的相同方法中不使用该参数。GFortran 抱怨没有被使用,并且我不想通过为特定文件制定特定规则(例如 )来使构建过程复杂化。composelinebreaking_array_compositorthis-Wno-unused-dummy-argument
要禁用有关某些选定的、未使用的虚拟参数的警告,我们可能需要执行一些“无操作”操作(例如虚拟赋值、IF 测试、获取地址等)。作为一种这样的方法,定义一个像这样的宏怎么样?
#define nop(x) associate( x => x ); end associate
Run Code Online (Sandbox Code Playgroud)
并将其用作
subroutine linebreaking_simple_compositor_compose(this)
class(linebreaking_simple_compositor), intent(in) :: this
nop( this )
print *, "Composing using a simple compositor."
end subroutine linebreaking_simple_compositor_compose
Run Code Online (Sandbox Code Playgroud)
在我的计算机上,ifort-14 和 gfortran >=4.8 接受此用法,不会使用 -warn 或 -Wall 发出警告。另一方面,Sun fortran 8.7 不接受这一点,因为还没有 ASSOCIATE 支持......(我真的希望它会支持后者!)
下面附上一个小测试代码:
module mymod
implicit none
type T
integer :: n
endtype
contains
subroutine mysub( this )
class(T) :: this
nop( this )
endsubroutine
subroutine mysub2( ptr )
type(T), pointer :: ptr
nop( ptr )
endsubroutine
end
program main
use mymod
type(T) :: a
type(T), pointer :: p
call mysub( a )
call mysub2( p )
endprogram
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2565 次 |
| 最近记录: |