为了进一步整理清楚C++语言的基础内容,我按照从简单到复杂的顺序,初步整理了C++语言元素层级体系,基础字符集只例举了C++语言内置的预定义的内容,从词汇表开始加入自定义的内容,最后梳理了从字符、词汇到语法,C++语言元素的层级体系,欢迎评论指正。
C++字符集
1.1 基本源字符集
C++标准定义的基本源字符集包含96个字符:
-
字母:a-z, A-Z
-
数字:0-9
-
空白符:空格、水平制表、垂直制表、换页、换行
-
图形字符:
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " '
ISO C++20标准截图
1.2 执行字符集扩展
执行字符集在基本源字符集基础上增加:
-
控制字符:警报、退格、回车等
-
空字符(null character)
-
实现定义的扩展字符
C++词汇表
C++语言词汇(Lexical)元素完整分类表
/* ========== 预定义内容 ========== */
// 1. 关键字系统 (C++20标准全部92个关键字)
alignas alignof and and_eq asm auto bitand bitor bool
break case catch char char8_t char16_t char32_t class
compl concept const consteval constexpr constinit const_cast
continue co_await co_return co_yield decltype default delete
do double dynamic_cast else enum explicit export extern false
float for friend goto if inline int long mutable namespace
new noexcept not not_eq nullptr operator or or_eq private
protected public register reinterpret_cast requires return
short signed sizeof static static_assert static_cast struct
switch template this thread_local throw true try typedef
typeid typename union unsigned using virtual void volatile
wchar_t while xor xor_eq
// 2. 预处理器符号系统
// 指令符号
#define #undef #include #if #ifdef #ifndef #else #elif #endif #line #error #pragma
// 操作符号
# ## _Pragma
// 预定义宏
__DATE__ __FILE__ __LINE__ __TIME__ __STDC__ __STDC_HOSTED__ __cplusplus
__STDC_UTF_16__ __STDC_UTF_32__ __STDC_MB_MIGHT_NEQ_WC__ __STDC_ISO_10646__
__STDCPP_STRICT_POINTER_SAFETY__ __STDCPP_THREADS__
// 3. 运算符系统 (全部38个可重载运算符)
// 算术运算符
+-*/ % ++ --
// 关系运算符
== != < > <= >=
// 逻辑运算符
&& || !
// 位运算符
&| ^ ~<< >>
// 赋值运算符
= += -= *= /= %= &= |= ^= <<= >>=
// 其他运算符
, ->* -> . :: ? : sizeof new delete
// 4. 标点符号系统
; {}[]() < > , : ... \" \' \\
// 5. 特殊保留标识符
main operator_new operator_delete operator_new[] operator_delete[]
typeid sizeof alignof noexcept dynamic_cast static_cast const_cast reinterpret_cast
thread_local co_await co_return co_yield
/* ========== 自定义内容 ========== */
// 1. 用户定义标识符规则
[a - zA - Z_][a - zA - Z0 - 9_] * // 正则表达式规则
// 2. 类型系统扩展
class MyClass {
using NestedType = int;
enum class Color { RED, BLUE };
template<typename U> struct NestedTemplate;
};
template<typename T> struct Box;
using StringMap = std::map<std::string, std::string>;
// 3. 运算符重载全集
struct OperatorOverloads {
// 算术运算符
T operator+(T rhs); T operator-(T rhs);
T operator*(T rhs); T operator/(T rhs);
T operator%(T rhs); T operator+();
T operator-();
// 自增/自减
T& operator++(); T operator++(int);
T& operator--(); T operator--(int);
// 位运算符
T operator&(T rhs); T operator|(T rhs);
T operator^(T rhs); T operator~();
T operator<<(int n); T operator>>(int n);
// 逻辑运算符
bool operator!(); bool operator&&(T rhs);
bool operator||(T rhs);
// 比较运算符
bool operator==(T rhs); bool operator!=(T rhs);
bool operator<(T rhs); bool operator<=(T rhs);
bool operator>(T rhs); bool operator>=(T rhs);
auto operator<=>(T rhs); // C++20三向比较
// 赋值运算符
T& operator=(T rhs); T& operator+=(T rhs);
T& operator-=(T rhs); T& operator*=(T rhs);
T& operator/=(T rhs); T& operator%=(T rhs);
T& operator&=(T rhs); T& operator|=(T rhs);
T& operator^=(T rhs); T& operator<<=(int n);
T& operator>>=(int n);
// 特殊运算符
R& operator*(); T* operator->();
R operator()(Args...); R& operator[](Idx);
operator U(); // 类型转换
};
// 4. 流运算符重载(需全局)
std::ostream& operator<<(std::ostream & os, const T & obj);
std::istream& operator>>(std::istream & is, T & obj);
// 5. 内存管理运算符
struct MemoryOps {
static void* operator new(size_t sz);
static void operator delete(void* ptr);
static void* operator new[](size_t sz);
static void operator delete[](void* ptr);
};
// 6. 用户定义字面量
struct Distance {
long double meters;
};
constexpr Distance operator""_km(long double val);
// 7. 类型特征扩展
template<typename T>
struct is_pointer {
static constexpr bool value = false;
template<typename U> struct rebind { typedef U* type; };
};
// 8. 宏定义扩展
#define DECLARE_TYPE(Name) class Name##_t { \
using self_type = Name##_t; \
static void static_func(); \
}
// 9. 模板特化扩展
template<>
struct is_pointer<void*> {
static constexpr bool value = true;
};
// 10. 概念约束 (C++20)
template<typename T>
concept Addable = requires(T a, T b) {
{ a + b } -> std::same_as<T>;
};
C++语言元素
语言元素
├── 字符(Character)
│ └── 预定义
│ └── ...
│ └── 自定义
│ └── ...
├── 词汇(Lexical)
│ └── 预定义
│ └── ...
│ └── 自定义
│ └── ...
└── 语法(Syntax)
└── 预定义
└── ...
└── 自定义
└── ...
/* ========== C++语言元素层级体系 ========== */
language_elements : {
/* 第一层:字符(Character) */
character : {
/* 预定义字符集 */
predefined : {
basic_source_charset : "96个基础字符(A-Z,a-z,0-9,标点等)",
execution_charset : "扩展字符集(含控制字符)",
universal_char_names : "\\uXXXX和\\UXXXXXXXX",
whitespace : "空格/换行/制表符等"
},
/* 自定义字符处理 */
user_defined : {
encoding_prefix : "u8/u/U/L等编码前缀",
raw_string_delimiter : "R\"(...)\"中的定界符"
}
},
/* 第二层:词汇(Lexical) */
lexical : {
/* 预定义词汇 */
predefined : {
keywords : "92个标准关键字(alignas/class等)",
operators : "38个运算符(+/-/<<等)",
punctuators : "; {} [] ()等分隔符",
literals : "42/3.14/'a'/\"str\"等字面量",
preprocessing_tokens : "#define/__LINE__等"
},
/* 自定义词汇 */
user_defined : {
identifiers : "变量/函数/类名等",
user_defined_literals : "operator\"\"_km",
macros : "#define MAX(a,b)",
alternative_tokens : "and/or/not等替代符号"
}
},
/* 第三层:语法(Syntax) */
syntax : {
/* 预定义语法结构 */
predefined : {
declarations : "int x; class X;等",
statements : "if/for/return等",
expressions : "x+y, func()等",
memory_model : "存储期/对象生命周期",
exception_handling : "try/catch/throw"
},
/* 自定义语法扩展 */
user_defined : {
class_definitions : "class/struct定义",
function_definitions : "函数实现",
template_metaprogramming : "模板元编程",
operator_overloads : "operator+等",
concept_definitions : "C++20概念约束"
}
}
}