Skip to content

FB_EVAL


编译器执行的内置定义(宏)。

语法

` FB_EVAL( arg )

`

参数

arg

参数

描述

在编译时对参数(常量表达式)求值。

当参数求值结果为字符串时,__FB_EVAL__ 以预处理器运算符格式返回字符串:

  • 非转义字符串字面量(形如:$"text"),

  • 转义字符串字面量(形如:!"text",如有需要)。

对于其他产生的数据类型,返回简单的 字面量(整数、浮点数、布尔值),不带前缀/后缀。

__FB_EVAL__ 宏适用于以下情况:

  • 存在非功能性表达式(即有副作用),

  • 需要在传递之前进行求值(即简化,遵循运算符优先级),

  • 在 fbc 不允许使用表达式的地方使用。

示例

start GeSHi

vb
#print 1 + 2
#print __FB_EVAL__( 1 + 2 )
#print 4 * Atn(1)
#print __FB_EVAL__( 4 * Atn(1) )

/' Compiler output:
1 + 2
3
4 * Atn(1)
3.141592653589793
'/

end GeSHi

start GeSHi

vb
'   In this example, the three '__FB_EVAL__' are absolutely mandatory in this 'assign()' macro.
'   Even for '__FB_QUOTE__( __FB_EVAL__( expr ) )', because for the case of expr = cos(1/x),
'   'cos(1/x)' must be properly evaluated before be quoted (after the previous 'assign("x", x+1)'),
'   otherwise in that case 'cos(1/x+1)' is taken into account (giving 'cos(2)') instead of 'cos(1/(x+1))' (giving 'cos (1/2)')
'   because the operator precedence is not applied by '__FB_QUOTE__'.

#macro assign( sym, expr )
    __FB_UNQUOTE__( __FB_EVAL__( "#undef " + sym ) )
    __FB_UNQUOTE__( __FB_EVAL__( "#define " + sym + " " + __FB_QUOTE__( __FB_EVAL__( expr ) ) ) )
#endmacro

#define x

assign( "x", 1 )
Print x

assign( "x", x+1 )
Print x

assign( "x", Cos(1/x) )
Print x

assign( "x", "hello" )
Print x

assign( "x", x+x )
Print x

/' Output:
 1
 2
 0.877582...
hello
hellohello
'/

end GeSHi

另请参阅 __FB_ARG_EXTRACT__ 示例。

版本

  • 自 fbc 1.08.0 起

与 QB 的区别

  • FreeBASIC 新增

另请参阅

  • __FB_QUOTE__
  • __FB_UNQUOTE__

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系