读书人

条件编译?解决方法

发布时间: 2013-01-23 10:44:49 作者: rapoo

条件编译?

#include <iostream> 
using namespace std;

enum {MAX=18};

void test()
{
#if MAX < 10 枚举元素不是常量吗,怎么这里不起作用?
cout<<"+++\n";
#endif
cout<<"---\n";
}
int main()
{
test();
return 0;
}

[解决办法]
预编译阶段进行的是扫描和替换,还不会去解析枚举常量。
[解决办法]
预编译不认识枚举。
[解决办法]
Most programs intended to run only on a NeXT computer won't need to use preprocessor conditionals.




Syntax of Conditionals

A conditional in the C preprocessor begins with a conditional command: #if, #ifdef, or #ifndef. These and a few related commands are described in the following sections.



The #if Command

The #if command in its simplest form consists of

#if expression
conditional-text
#endif /* expression */

The comment following the #endif isn't required, but it makes the code easier to read. Such comments should always be used, except in short conditionals that aren't nested. (Although you can put anything at all after the #endif and it will be ignored by the GNU C preprocessor, only comments are acceptable in ANSI Standard C.)

expression is a C expression of type int, subject to stringent restrictions. It may contain:

Integer constants, which are all regarded as long or unsigned long.
Character constants, which are interpreted according to the character set and conventions of the machine and operating system on which the preprocessor is running. The GNU C preprocessor uses the C data type char for these character constants; therefore, whether some character codes are negative is determined by the C compiler used to compile the preprocessor. If it treats char as signed, then character codes large enough to set the sign bit will be considered negative; otherwise, no character code is considered negative.
Character constants. The GNU C preprocessor uses the C data type char for these character constants.
Arithmetic operators for addition, subtraction, multiplication, division, bitwise operations, shifts, comparisons, &&, and


[解决办法]
.
Identifiers that aren't macros, which are all treated as 0.
Macro invocation. All macros in the expression are expanded before actual computation of the expression's value begins.

sizeof operators and enum-type values aren't allowed. enum-type values, like all other identifiers that aren't taken as macro invocations and expanded, are treated as 0.

The controlled text inside a conditional can include preprocessor commands. Then the commands inside the conditional are obeyed only if that branch of the conditional succeeds. The text can also contain other conditional groups. However, the #ifs and #endifs must balance.
[解决办法]
可以运行 的lz

[解决办法]

条件编译?解决方法
引用:
预编译阶段进行的是扫描和替换,还不会去解析枚举常量。


引用:
预编译不认识枚举。

[解决办法]
VC没研究过,即使可以也不能说明什么,因为它本身对C/C++的标准支持的不怎么样。

引用:
引用:预编译阶段进行的是扫描和替换,还不会去解析枚举常量。
我试过 怎么可以 vs2010?

[解决办法]
引用:
难道只能定义宏
#define MAX 18
这样也不行吧:const int MAX=18;

条件编译是在编译阶段就确定好了的,不是运行期。

读书人网 >C++

热点推荐