英文:
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 expr 和 sizeof (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.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论