6. 规则详解(3) 单行(SingleLine)
这些选项控制是否将短小的结构(函数/ if / 块 / lambda / enum / 循环 / namespace 等)合并成单行或保留多行
最终格式化结果取决于具体选项组合与其他换行设置(如 ColumnLimit、AllowShortFunctionsOnASingleLine 的枚举值等)
1. 短 Function
AllowShortFunctionsOnASingleLine,允许短函数单行,也就是短函数是否合并成单行。可选值:
None:从不合并Empty:只把空函数合并为一行(void f() {})InlineOnly:只合并内联函数(类内定义)Inline:合并内联且满足条件的其他函数All:尽可能把所有短函数合并到一行
以下演示格式化前后的效果。
- 效果演示
// 原始写法
int add (int x, int y) {
return x + y;
}
// None
int add (int x, int y) {
return x + y;
}
// All
int add(int x, int y) { return x + y; }
2. 短 if 语句
AllowShortIfStatementsOnASingleLine,控制短 if / else if / else 是否合并成单行,避免不必要的换行或使代码更紧凑。可选值:
Never:不允许把任何if合并成单行WithoutElse:允许把没有else的短if合并为单行,但出现else/else if时仍保持多行OnlyFirstIf:在if/else if链中,仅第一层if可以合并为单行,else if/else不会被合并为单行AllIfsAndElse:只要语句足够短,if、else if、else都可以合并成单行
以下演示格式化前后的效果。
- 效果一(AllowShortIfStatementsOnASingleLine: AllIfsAndElse)
#include <iostream>
void demo_short_ifs (int x) {
if ( x == 0 ) return;
if ( x == 1 ) std::cout << "do\n";
else std::cout << "other\n";
if ( x == 2 ) return;
else if ( x == 3 ) return;
else return;
}
- 效果二(AllowShortIfStatementsOnASingleLine: Never)
#include <iostream>
void demo_short_ifs (int x) {
if ( x == 0 )
return;
if ( x == 1 )
std::cout << "do\n";
else
std::cout << "other\n";
if ( x == 2 )
return;
else if ( x == 3 )
return;
else
return;
}
3. 短 case 标签
AllowShortCaseLabelsOnASingleLine,是否把短的 case 分支压缩成单行形式
- 效果一(AllowShortCaseLabelsOnASingleLine: true)
#include <iostream>
int short_case_demo (int n) {
switch ( n ) {
case 0: return 0;
case 1: return 1;
default: return -1;
}
}
int mixed_case_demo (int n) {
switch ( n ) {
case 0:
// 注释和多条语句会阻止单行合并
std::cout << "zero\n";
break;
case 1: std::cout << "one\n"; break;
default: return -1;
}
}
- 效果二(AllowShortCaseLabelsOnASingleLine: false)
#include <iostream>
int short_case_demo (int n) {
switch ( n ) {
case 0:
return 0;
case 1:
return 1;
default:
return -1;
}
}
int mixed_case_demo (int n) {
switch ( n ) {
case 0:
// 注释和多条语句会阻止单行合并
std::cout << "zero\n";
break;
case 1:
std::cout << "one\n";
break;
default:
return -1;
}
}
4. 短 枚举
AllowShortEnumsOnASingleLine,是否把短小的 enum / enum class 合并成单行(enum E { A, B };)
- 效果一(AllowShortEnumsOnASingleLine: true)
// 1) 普通枚举
enum ShortEnum { A, B };
// 2) 带注释的枚举成员
enum WithComment { C1, /* C1 comment */ C2 /* C2 comment */ };
// 3) 带初始化表达式
enum WithInit { D = 1000, E = 2000 };
- 效果二(AllowShortEnumsOnASingleLine: false)
// 1) 普通枚举
enum ShortEnum {
A,
B
};
// 2) 带注释的枚举成员
enum WithComment {
C1, /* C1 comment */
C2 /* C2 comment */
};
// 3) 带初始化表达式
enum WithInit {
D = 1000,
E = 2000
};
5. 短 命名空间
AllowShortNamespacesOnASingleLine,是否把短的 namespace 并成单行形式
5.1 效果一
- NamespaceIndentation:All
- AllowShortNamespacesOnASingleLine: true
#include <iostream>
// 短命名空间(默认 ShortNamespaceLines=1 行)
namespace Short { int value = 42; }
// 稍长的命名空间(超过 ShortNamespaceLines) → 会保持多行
namespace NotShort {
int a = 1;
int b = 2;
int sum() { return a + b; }
} // namespace NotShort
// 嵌套命名空间示例
namespace Outer {
namespace Inner { int x = 3; } // namespace Inner
} // namespace Outer
int main() {
std::cout << "Short.value = " << Short::value << "\n";
std::cout << "NotShort.sum() = " << NotShort::sum() << "\n";
std::cout << "Outer::Inner::x = " << Outer::Inner::x << "\n";
return 0;
}
5.2 效果二
- NamespaceIndentation:All
- AllowShortNamespacesOnASingleLine: false
#include <iostream>
// 短命名空间(默认 ShortNamespaceLines=1 行)
namespace Short {
int value = 42;
}
// 稍长的命名空间(超过 ShortNamespaceLines) → 会保持多行
namespace NotShort {
int a = 1;
int b = 2;
int sum() { return a + b; }
} // namespace NotShort
// 嵌套命名空间示例
namespace Outer {
namespace Inner {
int x = 3;
} // namespace Inner
} // namespace Outer
int main() {
std::cout << "Short.value = " << Short::value << "\n";
std::cout << "NotShort.sum() = " << NotShort::sum() << "\n";
std::cout << "Outer::Inner::x = " << Outer::Inner::x << "\n";
return 0;
}
6. 短 循环
AllowShortLoopsOnASingleLine,是否把短循环写成单行形式
注意:如果循环体使用大括号 { … },还受 AllowShortBlocksOnASingleLine 控制(决定是否把 {} 放在一行内)
6.1 效果一
- AllowShortLoopsOnASingleLine:true
- AllowShortBlocksOnASingleLine: Always
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3};
// 1) while 循环
int i = 0;
while (i < 3) ++i; // 单行语句
i = 0;
// 单行语句块 { 单行语句 }
while (i < 3) { ++i; }
// 2) for 循环(范围循环)
for (auto x: v) std::cout << x << ' '; // 单行语句
std::cout << '\n';
// 单行语句块 { 单行语句 }
for (auto x: v) { std::cout << x << ' '; }
std::cout << '\n';
// 3) do-while 循环
int c = 0;
// 单行语句块 { 单行语句 }
do { ++c; } while (c < 2);
std::cout << c << std::endl;
return 0;
}
6.2 效果二
- AllowShortLoopsOnASingleLine:false
- AllowShortBlocksOnASingleLine: Never
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3};
// 1) while 循环
int i = 0;
while (i < 3)
++i; // 单行语句
i = 0;
// 单行语句块 { 单行语句 }
while (i < 3) {
++i;
}
// 2) for 循环(范围循环)
for (auto x: v)
std::cout << x << ' '; // 单行语句
std::cout << '\n';
// 单行语句块 { 单行语句 }
for (auto x: v) {
std::cout << x << ' ';
}
std::cout << '\n';
// 3) do-while 循环
int c = 0;
// 单行语句块 { 单行语句 }
do {
++c;
} while (c < 2);
std::cout << c << std::endl;
return 0;
}
7. 短 语句块
AllowShortBlocksOnASingleLine,短的块 { … } 是否合并到同一行。它的值有三种:
Never:不合并,永远不要把块合并到同一行,哪怕块为空或只有一条语句Empty:只合并空块,只有空块 {} 会合并为单行(如 while(…) {});包含单条或多条语句的块保持多行Always:始终合并短块,对短块(例如只有一条语句的块或空块)尽量合并到同一行:while (c) { ++i; } 或 while (c) {}
说明:此选项针对“块”本身(即 { … }),并不是只针对 if / for / while,但常用于循环或控制语句的块。
7.1 效果一
- AllowShortLoopsOnASingleLine: true
- AllowShortBlocksOnASingleLine: Always
#include <iostream>
#include <vector>
int main() {
int i = 0;
// 1) 空块(Empty)
if (i == 0) {}
// 2) 单语句块(短块)
// 但是更是短循环,受 AllowShortLoopsOnASingleLine:false 控制,不会合并到一行
i = 0;
while (i < 1) { ++i; }
// 3) 多语句块(不应合并)
std::vector<int> v = {1, 2, 3};
for (auto x: v) {
std::cout << x;
std::cout << std::endl;
}
return 0;
}
7.2 效果二
- AllowShortLoopsOnASingleLine: false
- AllowShortBlocksOnASingleLine: Never
#include <iostream>
#include <vector>
int main() {
int i = 0;
// 1) 空块(Empty)
if (i == 0) {
}
// 2) 单语句块(短块)
i = 0;
while (i < 1) {
++i;
}
// 3) 多语句块(不应合并)
std::vector<int> v = {1, 2, 3};
for (auto x: v) {
std::cout << x;
std::cout << std::endl;
}
return 0;
}
8. 短 lambda 表达式
AllowShortLambdasOnASingleLine,允许短 lambda 表达式单行
- 效果一(AllowShortLambdasOnASingleLine: None)
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
// 1) 空 lambda
auto empty = []() {
};
// 2) 短 lambda(单个返回语句)作为算法参数
std::sort(v.begin(), v.end(), [](int a, int b) {
return a < b;
});
// 3) 多语句 lambda(不会合并为单行)
auto multi = [](int x) {
int y = x + 1;
return y * y;
};
std::cout << "multi(3) = " << multi(3) << '\n';
// 4) 捕获外部变量并返回(短体)
int offset = 10;
auto add_offset = [offset](int x) {
return x + offset;
};
std::cout << "add_offset(5) = " << add_offset(5) << '\n';
// 输出排序结果
for (auto n: v)
std::cout << n << ' ';
std::cout << '\n';
return 0;
}
- 效果二(AllowShortLambdasOnASingleLine: All)
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
// 1) 空 lambda
auto empty = []() {};
// 2) 短 lambda(单个返回语句)作为算法参数
std::sort(v.begin(), v.end(), [](int a, int b) { return a < b; });
// 3) 多语句 lambda(不会合并为单行)
auto multi = [](int x) {
int y = x + 1;
return y * y;
};
std::cout << "multi(3) = " << multi(3) << '\n';
// 4) 捕获外部变量并返回(短体)
int offset = 10;
auto add_offset = [offset](int x) { return x + offset; };
std::cout << "add_offset(5) = " << add_offset(5) << '\n';
// 输出排序结果
for (auto n: v)
std::cout << n << ' ';
std::cout << '\n';
return 0;
}







