# C++ 语法

C++ 语法是由 ISO C++ 标准定义的一套语法规则，用于规范 C++ 编程语言中声明、语句、表达式及其他构造的书写方式。一个 C++ 程序由一系列文本文件（通常是头文件和源文件）构成，这些文件包含声明；它们经过翻译成为可执行程序，当实现调用 `main` 函数时程序开始运行。<sup>[1](https://en.cppreference.com/cpp/language/basic_concepts)</sup> 该语言由 C 语言发展而来，其语法仍承袭着这一遗产：C++ 的设计者本贾尼·斯特劳斯特鲁普（Bjarne Stroustrup）曾称 C 的声明语法是“一次失败的实验”，并指出它过于宽松。<sup>[2](https://www.stroustrup.com/cuj_interview.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义文档 | 现行 ISO 标准为 C++23，正式编号 ISO/IEC 14882:2024(E)，是一份主要面向编译器和标准库实现者的技术文档<sup>[3](https://isocpp.org/std/the-standard)</sup> |
| 程序结构 | 程序是包含声明的文本文件；实体包括值、对象、引用、函数、类型、模板、模板特化、包（C++11）和命名空间<sup>[1](https://en.cppreference.com/cpp/language/basic_concepts)</sup> |
| 语句种类 | 标号语句、表达式语句、复合语句、选择语句、迭代语句、展开语句（C++26）、跳转语句、断言语句（`contract_assert`，C++26）、声明语句和 `try` 块<sup>[4](https://cppreference.com/cpp/language/statements)</sup> |
| 起源 | 工作始于 1979 年，当时名为“带类的C”（C with Classes）；C++ 这一名称于 1983 年底启用；首个商业版本于 1985 年 10 月发布；首个 ISO 标准为 C++98<sup>[5](https://stroustrup.com/bs_faq.html)</sup> |
| 已知的解析风险 | 声明/表达式歧义与模板声明/表达式歧义、“最令人烦恼的解析”，以及 C++11 之前的右移词法分析问题<sup>[6](https://mural.maynoothuniversity.ie/id/eprint/8232/)</sup><sup> • </sup><sup>[7](https://federico-busato.github.io/Modern-CPP-Programming/09.Object_Oriented_I.pdf)</sup><sup> • </sup><sup>[8](https://en.cppreference.com/cpp/language/angle_bracket_hack)</sup> |
| 最新语法 | C++26，由 WG21 于 2026 年 3 月批准，增加了合约（`contract_assert`）、展开语句（`template for`）、静态反射和执行模型<sup>[9](https://herbsutter.com/2026/03/29/c26-is-done-trip-report-march-2026-iso-c-standards-meeting-london-croydon-uk/)</sup><sup> • </sup><sup>[10](https://www.cppreference.com/cpp/language/template_for)</sup> |

## 声明与实体

声明将名字和实体引入程序。标准将名为“声明”的语法构造与 C++ 术语中的“声明”加以区分：一个声明可以是类说明符、枚举说明符、函数参数等，而引入某个名字的声明并不总是引入该名字所指实体的声明。<sup>[11](https://www.cppreference.com/cpp/language/declarations)</sup> 定义是为所引入实体提供完整定义的声明，且程序中任何被 ODR 使用的非内联函数或变量只能有一个定义。<sup>[1](https://en.cppreference.com/cpp/language/basic_concepts)</sup>

一个声明所能引入的实体包括值、对象、引用、结构化绑定（C++17）、结果绑定（C++26）、函数、枚举项、类型、类成员、模板、模板特化、包（C++11）和命名空间；预处理器宏不是 C++ 实体。<sup>[1](https://en.cppreference.com/cpp/language/basic_concepts)</sup> 名字通过名字查找与其声明相关联，在某个作用域内有效，并可能具有链接性，使其在不同翻译单元中指向同一实体。<sup>[1](https://en.cppreference.com/cpp/language/basic_concepts)</sup>

**声明符语法。** 所声明名字的类型由声明说明符序列加上声明符构成，而复杂的声明符将指针、数组和函数修饰符按优先级规则组合，括号可覆盖这些规则。在 `int *var[5];` 中，数组修饰符结合更紧，因此 `var` 是一个包含五个指向 `int` 的指针的数组；在 `int (*var)[5];` 中，括号颠倒了这一顺序，使 `var` 成为指向五个 `int` 的数组的指针。<sup>[12](https://learn.microsoft.com/en-us/cpp/c-language/interpreting-more-complex-declarators?view=msvc-170)</sup> 斯特劳斯特鲁普曾表示他更希望采用另一种方案：若不是受 C 兼容性的约束，声明语法本会设计得有所不同。<sup>[2](https://www.stroustrup.com/cuj_interview.html)</sup>

## 语句

语句是按顺序执行的程序片段，任何函数体都是一系列语句。<sup>[4](https://cppreference.com/cpp/language/statements)</sup> C++ 包括标号语句、表达式语句、复合语句、选择语句、迭代语句、跳转语句、声明语句和 `try` 块；典型程序中的大多数语句是表达式语句，如赋值或函数调用。<sup>[4](https://cppreference.com/cpp/language/statements)</sup> 工作草案的语法列出了语句类别，包括展开语句和断言语句，二者均为 C++26 新增。<sup>[13](https://eel.is/c++draft/stmt.pre)</sup>

C++26 引入了两种语句形式。`template for` 展开语句对包、花括号包围的表达式列表或可结构化绑定的对象进行迭代，每处理一个元素便将其语句体展开一次；它有枚举、迭代和解构三种形式。<sup>[10](https://www.cppreference.com/cpp/language/template_for)</sup> `contract_assert` 断言语句是合约设施的一部分，WG21 在合约先前被从 C++20 中撤出后投票将其纳入 C++26。<sup>[9](https://herbsutter.com/2026/03/29/c26-is-done-trip-report-march-2026-iso-c-standards-meeting-london-croydon-uk/)</sup>

## 歧义与解析风险

C++ 的语法充满了有歧义的推导，尤其是声明/表达式歧义和模板声明/表达式歧义，解析器前端必须使用上下文相关技术来解决这些歧义。<sup>[6](https://mural.maynoothuniversity.ie/id/eprint/8232/)</sup> 由于名字查找和重载决议依赖于声明，仅靠上下文无关文法无法完成解析；标准本身采用歧义消解规则来区分表达式和声明。<sup>[14](https://isocpp.org/files/papers/N4944.pdf)</sup>

**最令人烦恼的解析。** 诸如 `B b( A() );` 这样的声明，程序员可能意图将其视为一个以临时 `A` 对象初始化的 `B` 类型变量，却会被解释为一个名为 `b` 的函数声明，该函数接受一个类型为“返回 A 的函数”的参数，导致后续使用时出现编译错误。<sup>[7](https://federico-busato.github.io/Modern-CPP-Programming/09.Object_Oriented_I.pdf)</sup> 标准在声明子句中通过显式的歧义消解规则来处理此类情况。<sup>[15](https://eel.is/c++draft/dcl)</sup>

**尖括号 hack。** 在 [C++11](https://www.edgepedia.org/zh/c-11) 之前，当两个连续尖括号出现时，许多词法分析器会将它们解释为右移运算符，因此诸如 `A<B<int> >` 这样的嵌套模板参数列表需要在闭合尖括号之间加一个空格；C++11 使 `A<B<int>>` 成为合法写法。<sup>[8](https://en.cppreference.com/cpp/language/angle_bracket_hack)</sup>

## 模板、函数与模块

模板通过一个模板参数列表后跟类、函数、变量、别名或其他实体的声明来声明；自 C++20 起，模板参数列表还可以声明概念，形式为 `template < parameter-list > concept concept-name = constraint-expression ;`，而 requires 子句可以约束模板实参。<sup>[16](https://www.cppreference.com/cpp/language/templates)</sup> 函数将一系列语句与一个名字和参数列表关联；同一作用域中的多个函数在其参数列表不同时可以共享同一名字，这就是[函数重载](https://www.edgepedia.org/zh/function-overloading)；自 C++11 起，匿名函数以 lambda 表达式的形式书写。<sup>[17](https://en.cppreference.com/cpp/language/functions)</sup>

模块于 C++20 引入，是一种跨翻译单元共享声明和定义的语言特性，可替代头文件的部分用途。模块接口单元以 `export module name;` 开头，通过 `export` 声明导出实体，并通过 `import` 声明被使用；模块与命名空间相互正交。<sup>[18](https://en.cppreference.com/cpp/language/modules)</sup>

## 语法的演进

语法分层增长，与标准修订紧密相连。斯特劳斯特鲁普于 1979 年以“带类的C”开启了这门语言，旨在将 Simula 的程序组织设施与 C 的效率相结合；模板和异常处理于 20 世纪 80 年代后期加入，首个 ISO 标准 C++98 随后问世。<sup>[5](https://stroustrup.com/bs_faq.html)</sup> 自 2012 年起，ISO 委员会采用了一种解耦模式：主要特性先在技术规范中成熟，再合并入主干标准，从而允许标准按固定节奏发布。<sup>[19](https://isocpp.org/STD/STATUS)</sup>

C++26 于 2026 年 3 月的克罗伊登会议上获批，是近期最大的语法变更。它增加了合约、静态反射（P2996）、展开语句和 `std::execution` 异步模型，并移除了读取未初始化局部变量的未定义行为。<sup>[9](https://herbsutter.com/2026/03/29/c26-is-done-trip-report-march-2026-iso-c-standards-meeting-london-croydon-uk/)</sup>

甚至连标准的语法记法也在修订之中。一份 2026 年的提案（P3891）认为，该语法记法仅有连接、选择和可选展开三种操作，应当增加分组和重复运算符，从而消除大量递归的 `X-seq` 非终结符（例如 `declaration-seq: declaration declaration-seq_opt`），而代之以 `declaration_{seq}` 之类的形式。<sup>[20](https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p3891r1.html)</sup>

## 可读性与人因

语法的密度具有可测量的影响。一项比较 C++ 迭代器与 lambda 表达式的随机对照试验发现，使用 lambda 的参与者在时间、编译器错误或完成任务的能力方面均无优势，作者报告的证据表明，在其测试条件下，C++ 语法和错误可能产生人因方面的负面影响。<sup>[21](https://www.cs.kent.edu/~jmaletic/cs63902/Papers/Merlin16.pdf)</sup> 佐治亚理工学院的一篇关于可读性的学位论文指出，C++ 核心语法中的复杂元素（如运算符重载）会降低可读性。<sup>[22](https://repository.gatech.edu/bitstreams/3cae1a23-7562-4df6-b6d6-f86ed1253b9d/download)</sup> 这些发现与语言的设计约束相伴而行：斯特劳斯特鲁普曾写道，C++ 保留了 C 的优势和兼容性，代价是不再宣称语言完美，并接受了虽可管控但相当可观的语言复杂性。<sup>[23](https://stroustrup.com/hopl2.pdf)</sup>

## References

1. [Basic concepts, cppreference.com](https://en.cppreference.com/cpp/language/basic_concepts)
2. [C++: The Making of a Standard; Stroustrup Interviews](https://www.stroustrup.com/cuj_interview.html)
3. [The Standard, Standard C++ Foundation](https://isocpp.org/std/the-standard)
4. [Statements, cppreference.com](https://cppreference.com/cpp/language/statements)
5. [Bjarne Stroustrup: FAQ](https://stroustrup.com/bs_faq.html)
6. [Malloy, Gibbs, Power (2003). Decorating tokens to facilitate recognition of ambiguous language constructs. Software: Practice and Experience](https://mural.maynoothuniversity.ie/id/eprint/8232/)
7. [Federico Busato, Modern C++ Programming, Object-Oriented Programming I (lecture notes)](https://federico-busato.github.io/Modern-CPP-Programming/09.Object_Oriented_I.pdf)
8. [Angle Bracket hack, cppreference.com](https://en.cppreference.com/cpp/language/angle_bracket_hack)
9. [Herb Sutter (2026). C++26 is done! Trip report: March 2026 ISO C++ standards meeting](https://herbsutter.com/2026/03/29/c26-is-done-trip-report-march-2026-iso-c-standards-meeting-london-croydon-uk/)
10. [template for expansion (since C++26), cppreference.com](https://www.cppreference.com/cpp/language/template_for)
11. [Declarations, cppreference.com](https://www.cppreference.com/cpp/language/declarations)
12. [Interpreting More Complex Declarators, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/c-language/interpreting-more-complex-declarators?view=msvc-170)
13. [Statements preamble [stmt.pre], C++ working draft](https://eel.is/c++draft/stmt.pre)
14. [C++ Working Draft N4944](https://isocpp.org/files/papers/N4944.pdf)
15. [Declarations [dcl], C++ working draft](https://eel.is/c++draft/dcl)
16. [Templates, cppreference.com](https://www.cppreference.com/cpp/language/templates)
17. [Functions, cppreference.com](https://en.cppreference.com/cpp/language/functions)
18. [Modules (since C++20), cppreference.com](https://en.cppreference.com/cpp/language/modules)
19. [Current ISO C++ status, Standard C++ Foundation](https://isocpp.org/STD/STATUS)
20. [Jan Schultke (2026). Improve readability of the C++ grammar by adding a syntax for groups and repetitions (P3891R1)](https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p3891r1.html)
21. [Merlin, P. et al. (2016). An Empirical Study on the Impact of C++ Lambdas and Programmer Experience](https://www.cs.kent.edu/~jmaletic/cs63902/Papers/Merlin16.pdf)
22. [Martinez Nieto, dissertation on code readability, Georgia Tech](https://repository.gatech.edu/bitstreams/3cae1a23-7562-4df6-b6d6-f86ed1253b9d/download)
23. [Bjarne Stroustrup. A History of C++: 1979−1991](https://stroustrup.com/hopl2.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › C-family and imperative languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/c-syntax-epmnvqd1ym

English original: https://www.edgepedia.org/c-syntax-epmnvqd1ym
