条件编译没有cpp [英] Conditional compilation sans the cpp

查看:61
本文介绍了条件编译没有cpp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经毫不掩饰地说我真的不喜欢

C ++中的C预处理器。语言的任何方面都没有给我带来更多麻烦。没有任何方面

该语言导致我阅读的代码难以理解。

我把它描述为GOTO的类固醇,这就是它的本质!


反对废除它的一个论点是它在移植代码等时对条件

编译很有用。在我看来,C ++原生支持

。根据TC ++ PL(SE)§24.3.7.2,如果一个代码块是

用if(CONDITION){...}括起来整个表达式是编译的

away当CONDITION == false时。


这不能代替典型的#ifdef ... #endif用于

条件编译吗?

-

STH

哈顿定律:只有一个不可侵犯的法律

KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com

Mozilla: http://www.mozilla.org

I''ve made no secret of the fact that I really dislike the C preprocessor in
C++. No aspect of the language has caused me more trouble. No aspect of
the language has cause more code I''ve read to be difficult to understand.
I''ve described it as GOTO''s on steroids, and that''s what it is!.

One argument against abolishing it it that it is useful for conditional
compilation when porting code, etc. Well, it seems to me C++ supports that
natively. According to TC++PL(SE) §24.3.7.2 if a block of code is
bracketted with an if(CONDITION){...} the entire expression is "compiled
away" when CONDITION==false.

Can this not be used in place of the typical #ifdef ... #endif used for
conditional compilation?
--
STH
Hatton''s Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org

推荐答案

Steven T. Hatton写道:
Steven T. Hatton wrote:
我已经毫不掩饰地说我真的不喜欢C ++中的C预处理器。语言的任何方面都没有给我带来更多麻烦。语言没有任何方面导致我阅读的代码更难以理解。
我已将其描述为GOTO的类固醇,这就是它的本质!。

一个反对废除它的论点,它在移植代码等时对条件
编译很有用。好吧,在我看来C ++支持

本身。根据TC ++ PL(SE)§24.3.7.2,如果一个代码块用if(CONDITION){...}括号,则整个表达式被编译掉。当CONDITION == false时。

这不能代替用于
条件编译的典型#ifdef ... #endif吗?
I''ve made no secret of the fact that I really dislike the C preprocessor
in
C++. No aspect of the language has caused me more trouble. No aspect of
the language has cause more code I''ve read to be difficult to understand.
I''ve described it as GOTO''s on steroids, and that''s what it is!.

One argument against abolishing it it that it is useful for conditional
compilation when porting code, etc. Well, it seems to me C++ supports
that
natively. According to TC++PL(SE) §24.3.7.2 if a block of code is
bracketted with an if(CONDITION){...} the entire expression is "compiled
away" when CONDITION==false.

Can this not be used in place of the typical #ifdef ... #endif used for
conditional compilation?




这可以在很多地方使用,但是,有些情况下你的

想法不起作用,即当代码不在
$的范围内时b $ b函数:


(a)预处理器允许你有条件地包含文件。

(b)它允许更改函数的签名。

(c)它允许在一个班级中包括或排除某些成员。


可能还有更多。

最好的


Kai-Uwe Bux



This will work in many places, however, there are some instances where your
idea would not work, namely when the code is not within the body of a
function:

(a) the preprocessor allows you to conditionally include files.
(b) it allows to change the signature of a function.
(c) it allows to include or exclude certain members in a class.

Probably, there is way more.
Best

Kai-Uwe Bux





Steven T. Hatton写道:


Steven T. Hatton wrote:
我毫不掩饰我真的不喜欢C ++中的C预处理器。语言的任何方面都没有给我带来更多麻烦。语言没有任何方面导致我阅读的代码更难以理解。
我已将其描述为GOTO的类固醇,这就是它的本质!

反对废除它的一个论点是它在移植代码等时对条件
编译很有用。好吧,在我看来C ++原生支持它。根据TC ++ PL(SE)§24.3.7.2,如果一个代码块用if(CONDITION){...}括号,则整个表达式被编译掉。当CONDITION == false时。

这不能代替用于
条件编译的典型#ifdef ... #endif吗?
I''ve made no secret of the fact that I really dislike the C preprocessor in
C++. No aspect of the language has caused me more trouble. No aspect of
the language has cause more code I''ve read to be difficult to understand.
I''ve described it as GOTO''s on steroids, and that''s what it is!.

One argument against abolishing it it that it is useful for conditional
compilation when porting code, etc. Well, it seems to me C++ supports that
natively. According to TC++PL(SE) §24.3.7.2 if a block of code is
bracketted with an if(CONDITION){...} the entire expression is "compiled
away" when CONDITION==false.

Can this not be used in place of the typical #ifdef ... #endif used for
conditional compilation?




#ifdef开发

#ifdef USE_DEBUG

#define DEBUG_FUNC(FUNC)FUNC

#else

#define DEBUG_FUNC(FUNC)

#endif

#endif


int sort(class Collection& col,sortType ()
{
(check_sorted(col,type));

}


比以下更简洁:


int sort( class Collection& col,sortType type)

{

//对集合进行排序一些如何

...

if(DEBUG_FUNC){

check_sorted(col,type);

}

}

,你仍然有如何让编译器知道

DEBUG_FUNC总是假的问题?


然后还有< ;C断言>或者你最喜欢的替代品。



#ifdef DEVELOPMENT
#ifdef USE_DEBUG
#define DEBUG_FUNC(FUNC) FUNC
#else
#define DEBUG_FUNC(FUNC)
#endif
#endif

int sort(class Collection& col, sortType type)
{
// sort the collection some how
...

DEBUG_FUNC(check_sorted(col,type));
}

is less clutter than:

int sort(class Collection& col, sortType type)
{
// sort the collection some how
...

if (DEBUG_FUNC) {
check_sorted(col,type);
}
}

and you still have the problem of how to get the compiler to know that
DEBUG_FUNC is always false?

Then there is also <cassert> or your favourite replacement.


" Steven T. Hatton"写道:
"Steven T. Hatton" wrote:

我毫不掩饰我真的不喜欢C ++中的C预处理器。语言的任何方面都没有给我带来更多麻烦。语言没有任何方面导致我阅读的代码更难以理解。
我已将其描述为GOTO的类固醇,这就是它的本质!

反对废除它的一个论点是它在移植代码等时对条件
编译很有用。好吧,在我看来C ++原生支持它。根据TC ++ PL(SE)§24.3.7.2,如果一个代码块用if(CONDITION){...}括号,则整个表达式被编译掉。当CONDITION == false时。


但它仍然通过编译器运行。

这不能代替典型的#ifdef ... #endif用于
条件编译?

I''ve made no secret of the fact that I really dislike the C preprocessor in
C++. No aspect of the language has caused me more trouble. No aspect of
the language has cause more code I''ve read to be difficult to understand.
I''ve described it as GOTO''s on steroids, and that''s what it is!.

One argument against abolishing it it that it is useful for conditional
compilation when porting code, etc. Well, it seems to me C++ supports that
natively. According to TC++PL(SE) §24.3.7.2 if a block of code is
bracketted with an if(CONDITION){...} the entire expression is "compiled
away" when CONDITION==false.
But it nevertheless runs through the compiler.

Can this not be used in place of the typical #ifdef ... #endif used for
conditional compilation?




并非在所有情况下。


考虑:

你想要编写一个处理目录的函数。在一个

系统上,对此的调用被调用,例如。 GetFirstFile,GetNextFile。

在另一个系统上调用相同的调用,例如。 GetFirst,GetNext。

在第三个系统上,整个机制以完全不同的方式工作。

因此,重点在于:系统A有GetFirstFile函数和

GetNextFile,这些函数甚至不能在系统B或系统C上使用。所以

你需要一种'隐藏'的方法'来自编译器的那些函数调用。


#ifdef SYSTEM_A


i = GetFirstFile(目录);

而(i){

i = GetNextFile(FileName);

处理(FileName);

}


#else if SYSTEM_B

i = GetFirst(目录);

while(i){

i = GetNext(FileName) ;

处理(FileName);

}


#else如果SYSTEM_C


ReadDirectory(目录,DirStruct);

for(i = 0; i< DirStruct.NrEntries; ++ i)

流程(DirStruct(File [i]) ;


#endif


Whe编译SYSTEM_A你实际上需要编译器甚至不需要
*看看SYSTEM_B或SYSTEM_C中的实现代码,因为SYSTEM_A

简单没有那个函数可用。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


这篇关于条件编译没有cpp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆