为什么preprocessor使用率比C / C ++ / ObjC其他语言不常见? [英] Why is preprocessor usage less common in languages other than C/C++/ObjC?

查看:148
本文介绍了为什么preprocessor使用率比C / C ++ / ObjC其他语言不常见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Java和VB.Net程序员约4年,约6个月的C#程序员。我也用了一堆像Perl,Python和PHP和JavaScript动态语言的。

I've been a Java and VB.Net programmer for about 4 years and a C# programmer for about 6 months. I've also used a bunch of dynamic languages like Perl, Python, PHP, and JavaScript.

我从来没有需要一个preprocessor。

I've never had a need for a preprocessor.

我的问题是:你为什么看到这样大量使用C,C ++和Objective-C preprocessors的却很少(或从不)看到它在如Java,C#或斯卡拉语言

My question is: why do you see such extensive use of preprocessors in C, C++, and Objective-C but rarely (or never) see it in languages like Java, C#, or Scala?

推荐答案

我不知道Objective-C的,所以我的答案是关于对比使用C和C ++中的preprocessor的。

I don't know Objective-C, so my answer will be about contrasting the use of the preprocessor in C and C++.

在preprocessor原本对于C需要有几个原因。如果我没有记错,原本Ç没有常量,所以的#define需要避免幻数。此前1999 C没有内联函数,如此反复的#define用于创建宏或伪功能来保存函数调用的开销,同时保持code结构。 C也没有运行时或编译时多态,所以还需要条件编译的#ifdefs。编译器是典型的没有足够的智慧优化掉可达code,所以,再次的#ifdefs被用来将调试或诊断code。

The preprocessor was originally necessary for C for several reasons. If I remember correctly, originally C did not have constants, so #define was needed to avoid magic numbers. Prior to 1999 C did not have inline functions, so again #define was used to create macros or "pseudo-functions" to save the overhead of a function call, while keeping code structured. C also doesn't have run-time or compile-time polymorphism, so #ifdefs were needed for conditional compilation. Compilers were typically not smart enough to optimize away unreachable code, so, again, #ifdefs were used to insert debugging or diagnostic code.

使用C ++中的preprocessor是倒退到C,并在一般不赞成。语言功能,如常量,内联函数和模板可以在使用C你会使用preprocessor大多数情况下使用。

Using the preprocessor in C++ is a throwback to C, and is generally frowned upon. Language features, such as constants, inline functions, and templates can be used in most situations where in C you would have used the preprocessor.

在少数情况下,使用pre处理器在C ++中是可以接受的,甚至必要包括头文件的卫兵,以prevent被多次包括相同的标题,的#ifdef __cplusplus 用于C和C ++,相同的标题__FILE__ __LINE__和日志记录,以及其他等等。

The few cases where the use of a pre-processor in C++ is acceptable or even necessary include the guards for the header files, to prevent the same header from being included multiple times, #ifdef __cplusplus to use the same header for both C and C++, __FILE__ and __LINE__ for logging, and a few others.

在preprocessor也经常用于特定于平台的定义,虽然 C ++陷阱由斯蒂芬·杜赫斯特的建议具有特定于平台的定义不同的include目录,并利用他们在不同的生成配置为每个平台。

The preprocessor is also often used for platform-specific definitions, although C++ Gotchas by Stephen Dewhurst advises having separate include directories for the platform specific definitions, and using them in separate build configurations for each platform.

这篇关于为什么preprocessor使用率比C / C ++ / ObjC其他语言不常见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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