c – 为什么要使用预处理器指令进行“案例”声明?

前端之家收集整理的这篇文章主要介绍了c – 为什么要使用预处理器指令进行“案例”声明?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在浏览SpiderMonkey引擎源,并在解释器中看到一些令我感兴趣的代码.
// Portable switch-based dispatch.
# define INTERPRETER_LOOP()       the_switch: switch (switchOp)
# define CASE(OP)                 case OP:
# define DEFAULT()                default:

(来源:https://dxr.mozilla.org/mozilla-b2g44_v2_5/source/js/src/vm/Interpreter.cpp#1579)

定义类似案例OP的任何非风格的好处:作为CASE(OP)?

@R_404_323@

Look up半屏:
#if (defined(__GNUC__) ||                                                         \
     (__IBMC__ >= 700 && defined __IBM_COMPUTED_GOTO) ||                      \
     __SUNPRO_C >= 0x570)
// Non-standard but faster indirect-goto-based dispatch.
# define INTERPRETER_LOOP()
# define CASE(OP)                 label_##OP:
// ... <snip>
#else
// Portable switch-based dispatch.
# define INTERPRETER_LOOP()       the_switch: switch (switchOp)
# define CASE(OP)                 case OP:
// ... <snip>
#endif

GCC和其他一些编译器支持“computed goto”,即faster than a loop-switch for an interpreter loop,但是是非标准的,因此不可移植.

如果编译器支持计算的goto,则#if的第一个分支定义了INTERPRETER_LOOP,CASE(OP)等,以使用计算的goto;否则,#else分支根据标准设施来定义它们.

原文链接:https://www.f2er.com/c/111866.html

猜你在找的C&C++相关文章