mat*_*ath 22 c++ code-formatting indentation
当有许多预处理器语句和许多#ifdef级联时,很难得到概述,因为通常它们不会缩进.例如
#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#ifdef SOMEOTHER
stmts
#endif
maybe stmts
#endif
Run Code Online (Sandbox Code Playgroud)
当我考虑缩进这些预处理器语句时,我担心会与一般缩进级别混淆.那么你如何以一种美丽的方式解决这个问题呢?
Fab*_*llo 20
就像你一样,我还没有考虑最好的缩进方式,但我在不止一个地方找到了这个替代缩进,其中#总是放在第一列,只是关键字是缩进的:
#ifdef __WIN32__
# include <pansen_win32>
#else
# include <..>
#endif
Run Code Online (Sandbox Code Playgroud)
在Visual Studio中,当您键入#作为第一个字符时,它总是将其缩进到左侧,因此MS似乎倾向于从不缩进预处理程序语句,或使用上述格式.
当你将非预处理器和预处理器语句混合并应用缩进时,最大的问题是.无论哪种选择,都很难使代码看起来很好:
选项(a)
for (...)
{
if (foo)
{
if (bar)
{
#ifdef __WIN32__
c = GetTickCount();
#else
c = clock();
#endif
}
}
}
Run Code Online (Sandbox Code Playgroud)
选项(b)
for (...)
{
if (foo)
{
if (bar)
{
#ifdef __WIN32__
c = GetTickCount();
#else
c = clock();
#endif
}
}
}
Run Code Online (Sandbox Code Playgroud)
选项(c)
for (...)
{
if (foo)
{
if (bar)
{
# ifdef __WIN32__
c = GetTickCount();
# else
c = clock();
# endif
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这一点上,它成为个人品味的问题,就像许多其他缩进风格一样.
Jam*_*lis 19
只是因为预处理指令"通常"没有缩进不是一个不缩进它们的好理由:
#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#ifdef SOMEOTHER
stmts
#endif
maybe stmts
#endif
Run Code Online (Sandbox Code Playgroud)
如果您经常对预处理指令进行多级嵌套,则应重新编写它们以使其更简单.
这个问题已经有很多有效的答案了。对于那些想要视觉图像的人来说,这就是。
在 Visual Studio 中,转到选项搜索indent并选择您的语言。就我而言,它是 c++。当您在选项之间切换时,Visual Studio 将向您显示以下示例。