在`sizeof`后面如何保留空格?

huangapple go评论91阅读模式
英文:

How to keep space after `sizeof`?

问题

默认情况下,clang-format 将 sizeof 放在第一组而不是第二组。是否有办法在单词 sizeof 后留出一个空格?我这样设置的主要原因是以下表达式:

sizeof buffer / sizeof (int)

在第一个 sizeof 后留出一个空格,但在第二个 sizeof 后不留空格,感觉相当不一致。

英文:

Basically the title. I want my spacing rules to produce this:

#define MACRO_FOO(x) x
MACRO_FOO(2)
int foo(int x);
foo(2);

if (condition) {
    ...
}
while (false) {
    ...
}
sizeof (int)

By default, clang-format puts sizeof in the first group, not the second. Is there any way to leave a space after the word sizeof?

The main reason I have for spacing it like that is expressions such as the following:

sizeof buffer / sizeof (int)

where having a space after the first sizeof but not the second feels rather inconsistent.

答案1

得分: 3

https://clang.llvm.org/docs/ClangFormatStyleOptions.html 上查看,我看到:

  • SpaceBeforeParensOptions 没有相关标志。
  • 没有相关的 ...SpaceAfter... 选项。
  • 没有提到 sizeof

因此,我认为目前无法配置 clang-format 以始终在 sizeof 后添加空格。

(虽然我强烈不建议使用 sizeof (Type),因为它不太安全,但我个人更喜欢在 sizeof 后面添加一个空格,以便一致处理 sizeof exprsizeof (expr),以便更明显地将 sizeof 视为一种语言结构而不是函数(或函数样式的宏)调用。考虑提交一个问题。)

英文:

Looking over https://clang.llvm.org/docs/ClangFormatStyleOptions.html, I see:

  • No relevant flags for SpaceBeforeParensOptions.
  • No relevant ...SpaceAfter... options.
  • Nothing that mentions sizeof.

Therefore, I think it's simply currently not possible to configure clang-format to always put a space after sizeof.

(While I strongly discourage the use of sizeof (Type) because it's less safe, I personally do prefer having a space after sizeof so that sizeof expr and sizeof (expr) are treated consistently and so that sizeof is more obviously a language construct and not a function (or function-like macro) call. Consider filing an issue.)

huangapple
  • 本文由 发表于 2023年3月1日 13:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定