#if的行为 [英] Behaviour of #if
问题描述
对下面代码的正确编译器响应是什么?我很乐意和默默地编译这个
(自然地将#if评估为真)。错误
会有所帮助。
// MONITOR_CORE.CPP
// oops,忘了#include标题#定义
//常数REACTOR_TYPE& REACTOR_OLD_MODEL
void MonitorReactorCore()
{
#if REACTOR_TYPE == REACTOR_NEW_MODEL
//无事可做
//由硬件监控,自动关机
#else // REACTOR_OLD_MODEL
if(CoreOverTemperature())
ShutDownReactor();
#endif
//更多监控代码
}
David
PS我无法在The C ++ Programming Language中找到答案。 (第3页
ed。)
What is the correct compiler response to the code below? Mine compiles this
happily and silently (with the #if evaluating to true, naturally). An error
would be helpful.
// MONITOR_CORE.CPP
// oops, forgot to #include header that #defines
// constants REACTOR_TYPE & REACTOR_OLD_MODEL
void MonitorReactorCore()
{
#if REACTOR_TYPE == REACTOR_NEW_MODEL
// nothing to do
// monitored by hardware, with auto-shutdown
#else // REACTOR_OLD_MODEL
if(CoreOverTemperature())
ShutDownReactor();
#endif
// more monitoring code
}
David
P.S. I could not locate the answer in "The C++ Programming Language" (3rd
ed.)
推荐答案
" David White" < no.email@provided>在消息中写道
新闻:Io ****************** @ nasal.pacific.net.au ...
"David White" <no.email@provided> wrote in message
news:Io******************@nasal.pacific.net.au...
//常量REACTOR_TYPE& REACTOR_OLD_MODEL
// constants REACTOR_TYPE & REACTOR_OLD_MODEL
REACTOR_NEW_MODEL应该是
David
REACTOR_NEW_MODEL that should be
David
2003年7月3日星期四20:18:44 +1000,David White写道:
On Thu, 03 Jul 2003 20:18:44 +1000, David White wrote:
对下面的代码有什么正确的编译器响应?我很乐意和无声地编译它(#if评估为真,自然)。错误
会有所帮助。
// MONITOR_CORE.CPP
// oops,忘了#include标题#defines
//常量REACTOR_TYPE& REACTOR_OLD_MODEL
#ifndef REACTOR_TYPE
#error REACTOR_TYPE未定义
#endif
void MonitorReactorCore( )
[snip]
PS我无法在The C ++ Programming Language中找到答案。 (第3版)。
What is the correct compiler response to the code below? Mine compiles this
happily and silently (with the #if evaluating to true, naturally). An error
would be helpful.
// MONITOR_CORE.CPP
// oops, forgot to #include header that #defines
// constants REACTOR_TYPE & REACTOR_OLD_MODEL
#ifndef REACTOR_TYPE
#error REACTOR_TYPE not defined
#endif
void MonitorReactorCore() [snip]
P.S. I could not locate the answer in "The C++ Programming Language" (3rd
ed.)
第813页
hth
NPV
page 813
hth
NPV
David White写道:
David White wrote:
对下面的代码有什么正确的编译器响应?我的
快乐而安静地编译(#if评估为真,
自然)。错误会有所帮助。
#if REACTOR_TYPE == REACTOR_NEW_MODEL
What is the correct compiler response to the code below? Mine
compiles this happily and silently (with the #if evaluating to true,
naturally). An error would be helpful. #if REACTOR_TYPE == REACTOR_NEW_MODEL
预处理器会将任何标识符转换为0,这些标识符在宏扩展后仍然存在,但是在扩展之前表达式被评估。所以,如果名字不是
宏,你得到一个这样的表达式:
#if 0 == 0
问候,
Paul Mensonides
The preprocessor will convert any identifiers to 0 that remain after macro
expansion but before the expression is evaluated. So, if the names aren''t
macros, you get an expression like this:
#if 0 == 0
Regards,
Paul Mensonides
这篇关于#if的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!