如何缩进预处理器语句?

发布于 2024-09-04 10:03:54 字数 257 浏览 4 评论 0原文

当有许多预处理器语句和许多 #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温馨耳语 2024-09-11 10:03:54

仅仅因为预处理指令“通常”不缩进,并不是不缩进它们的好理由:

#ifdef __WIN32__
    #include <pansen_win32>
#else
    #include <..>
    #ifdef SOMEOTHER
        stmts
    #endif
    maybe stmts
#endif

如果您经常有多层嵌套的预处理指令,则应该重新设计它们以使其更简单。

Just because preprocessing directives are "normally" not indented is not a good reason not to indent them:

#ifdef __WIN32__
    #include <pansen_win32>
#else
    #include <..>
    #ifdef SOMEOTHER
        stmts
    #endif
    maybe stmts
#endif

If you frequently have multiple levels of nesting of preprocessing directives, you should rework them to make them simpler.

眼睛会笑 2024-09-11 10:03:54

和你一样,我还没有决定缩进的最佳方式,但我在不止一个地方发现了这种替代缩进,其中 # 始终放置在第一列,并且仅缩进关键字:

#ifdef __WIN32__
#  include <pansen_win32>
#else
#  include <..>
#endif

在 Visual Studio 中,当您键入 # 作为第一个字符时,它总是将其缩进带到左侧,因此看起来 MS 要么倾向于不缩进预处理器语句,要么使用上述格式。

最大的问题是当您混合使用非预处理器和预处理器语句并应用缩进时。无论哪种选择,都很难使代码看起来不错:

选项 (a)

for (...)
{
  if (foo)
  {
    if (bar)
    {
#ifdef __WIN32__
      c = GetTickCount();
#else
      c = clock();
#endif
    }
  }
}

选项 (b)

for (...)
{
  if (foo)
  {
    if (bar)
    {
      #ifdef __WIN32__
      c = GetTickCount();
      #else
      c = clock();
      #endif
    }
  }
}

选项 (c)

for (...)
{
  if (foo)
  {
    if (bar)
    {
#     ifdef __WIN32__
        c = GetTickCount();
#     else
        c = clock();
#     endif
    }
  }
}

在这一点上,就像许多其他缩进样式一样,它变成了个人品味的问题。

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:

#ifdef __WIN32__
#  include <pansen_win32>
#else
#  include <..>
#endif

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)

for (...)
{
  if (foo)
  {
    if (bar)
    {
#ifdef __WIN32__
      c = GetTickCount();
#else
      c = clock();
#endif
    }
  }
}

option (b)

for (...)
{
  if (foo)
  {
    if (bar)
    {
      #ifdef __WIN32__
      c = GetTickCount();
      #else
      c = clock();
      #endif
    }
  }
}

option (c)

for (...)
{
  if (foo)
  {
    if (bar)
    {
#     ifdef __WIN32__
        c = GetTickCount();
#     else
        c = clock();
#     endif
    }
  }
}

At this point, it becomes a question of personal taste, as so many other indentation styles.

原谅我要高飞 2024-09-11 10:03:54

这个问题已经有很多有效的答案了。对于那些想要视觉图像的人来说,这就是。

在 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.

enter image description here

心欲静而疯不止 2024-09-11 10:03:54

首先,确保您确实需要所有 ifdef 语句。也许有一种方法可以重构它们以限制嵌套 if 检查的数量?假设您确实需要它们:

最好将第一部分(包含)分成一个单独的包含文件:

your_header.h 中:

#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#endif

然后在实现文件中,您可以保留内容更理智了。自由空行和注释是我在非缩进 #ifdef 检查时通常采用的方法。

#ifndef __WIN32__
#ifdef SOMEOTHER

stmts

// SOMEOTHER
#endif

maybe stmts

// __WIN32__
#endif

尽管如此,如果预处理器检查使代码更具可读性,则没有规定不能缩进。

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:

#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#endif

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.

#ifndef __WIN32__
#ifdef SOMEOTHER

stmts

// SOMEOTHER
#endif

maybe stmts

// __WIN32__
#endif

All that said, there's no rule that you can't indent preprocessor checks if it makes the code more readable.

国际总奸 2024-09-11 10:03:54

“话虽如此,如果预处理器检查使代码更具可读性,则没有规定不能缩进。”

不,但在样式受控制且代码经过调整以匹配的世界中,启用样式缩进 #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 like if 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 #ifs can not be accommodated for in the styling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文