应该preprocessor指令是某一行的开始? [英] Should preprocessor instructions be on the beginning of a line?
问题描述
前段时间我发现一个(而古)C编译器,它扫描宏这样(伪code):
A while ago I have discovered an (rather ancient) C Compiler, which scanned macros this way (Pseudo code):
if line.startswith("#include") or line.startswith("#define"):
...
..哪位好心的提出,其中宏应该被放置在一行的开始,像这样对我的问题是:
.. Which kind of raised the question for me where macros should really be placed, at the beginning of a line, like so:
void stuff()
{
#if defined(WIN32) || defined(_WIN32)
...
#else
#if defined(__GNUC__)
...
#else
...
#endif
#endif
}
或者说像这样(因为这是我做的,为了提高可读性的方式):
Or rather like so (as that's the way I do it, for improved readability):
void stuff()
{
#if defined(WIN32) || defined(_WIN32)
...
#else
# if defined(__GNUC__)
...
# else
...
# endif
#endif
}
是一个缩进preprocessor code标准化的方式,那就是,无论我怎么缩进它,它总是会以同样的方式?
Is the way one indents the Preprocessor code standardized, that is, no matter how i indent it, it will always work the same way?
推荐答案
需要一些旧的C编译器的的#define
(例如)与左边距齐平:
Some old C compilers required that the #define
(for example) be flush with the left margin:
#define FOO bar
规定的其他C编译器只有在#
处于左边距,所以,你可以:
Other C compilers required only that the #
be at the left margin, so you could:
# define FOO bar
较新的C编译器倾向于接受#
任何前导空格后:
#define FOO bar
如果你想用这种老的编译器的兼容性,你至少应该把你的#
中的第一列。如果兼容性并不重要,那么它给你。
If you want compatibility with such older compilers, you should at least put your #
in the first column. If compatibility doesn't matter, then it's up to you.
我通常会尽量不嵌入 #IFDEF
块内部的功能,所以是否应缩进大多是整个问题就会消失。
I would usually try not to embed #ifdef
blocks inside functions, so the whole question of whether they should be indented mostly goes away.
这篇关于应该preprocessor指令是某一行的开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!