如何缩进预处理器语句?
当有许多预处理器语句和许多 #ifdef 级联时,很难获得概览,因为它们通常不缩进。例如,
#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#ifdef SOMEOTHER
stmts
#endif
maybe stmts
#endif
当我考虑缩进这些预处理器语句时,我担心会与一般缩进级别混淆。那么如何以漂亮的方式解决这个问题呢?
When there are many preprocessor statements and many #ifdef cascades, it's hard to get an overview since normally they are not indented. e.g.
#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#ifdef SOMEOTHER
stmts
#endif
maybe stmts
#endif
When I consider also indenting those preprocessor statements, I fear of getting confused with the general indentation level. So how do you solve this in a beautiful way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅仅因为预处理指令“通常”不缩进,并不是不缩进它们的好理由:
如果您经常有多层嵌套的预处理指令,则应该重新设计它们以使其更简单。
Just because preprocessing directives are "normally" not indented is not a good reason not to indent them:
If you frequently have multiple levels of nesting of preprocessing directives, you should rework them to make them simpler.
和你一样,我还没有决定缩进的最佳方式,但我在不止一个地方发现了这种替代缩进,其中 # 始终放置在第一列,并且仅缩进关键字:
在 Visual Studio 中,当您键入 # 作为第一个字符时,它总是将其缩进带到左侧,因此看起来 MS 要么倾向于不缩进预处理器语句,要么使用上述格式。
最大的问题是当您混合使用非预处理器和预处理器语句并应用缩进时。无论哪种选择,都很难使代码看起来不错:
选项 (a)
选项 (b)
选项 (c)
在这一点上,就像许多其他缩进样式一样,它变成了个人品味的问题。
As you, I didn't make my mind yet about the best way to indent, but I found in more than one place this alternative indentation in which the # is placed always at the first column and just the keyword is indented:
In Visual Studio, when you type # as the first character it always brings its indentation to the left, so it seems that MS either favors never indenting preprocessor statements, or using the above format.
The big problem is when you have non-preprocessor and preprocessor statements mixed and indentation applied. It's hard to make code that looks good, no matter which option:
option (a)
option (b)
option (c)
At this point, it becomes a question of personal taste, as so many other indentation styles.
这个问题已经有很多有效的答案了。对于那些想要视觉图像的人来说,这就是。
在 Visual Studio 中,转到选项搜索
indent
并选择您的语言。就我而言,它是 c++。当您在选项之间切换时,Visual Studio 将向您显示以下示例。This question already has lots of valid answers. For people that want a visual image here it is.
In visual studio go to options search for
indent
and select your language. In my case it is c++. As you toggle between the options visual studio will show you an example bellow.首先,确保您确实需要所有
ifdef
语句。也许有一种方法可以重构它们以限制嵌套 if 检查的数量?假设您确实需要它们:最好将第一部分(包含)分成一个单独的包含文件:
在
your_header.h
中:然后在实现文件中,您可以保留内容更理智了。自由空行和注释是我在非缩进
#ifdef
检查时通常采用的方法。尽管如此,如果预处理器检查使代码更具可读性,则没有规定不能缩进。
First, make sure you actually need all the
ifdef
statements. Perhaps there's a way to refactor them to keep the number of nested if checks limited? Assuming you do need them:It would be a good idea to separate the first portion (the includes) into a separate include file:
In
your_header.h
:Then in the implementation file, you can then keep things more sane. Liberal blank lines and comments are the approach I've typically taken regarding non-indented
#ifdef
checks.All that said, there's no rule that you can't indent preprocessor checks if it makes the code more readable.
“话虽如此,如果预处理器检查使代码更具可读性,则没有规定不能缩进。”
不,但在样式受控制且代码经过调整以匹配的世界中,启用样式缩进
#if
语句(如if
语句)会很好。虽然
#if
在技术上可能是一种不同的语言,但它是 C/C++ 规范的一部分,并且由于它不是任何当前关键字的字母对字母匹配,因此没有理由#if< /code> 不能容纳在样式中。
"All that said, there's no rule that you can't indent preprocessor checks if it makes the code more readable."
No, but in a world where style is controlled and the code is adjusted to match it would be nice to enable the style to indent
#if
statements likeif
statements.While
#if
might technically be a different language it is part of the C/C++ specification and as it is not a letter for letter match of any current keywords there is no reason that#if
s can not be accommodated for in the styling.