C++鲜为人知的符号
目录
目录 1
1. 引言 1
2. 少为人知的符号表1 1
2.1. 符号表 1
2.2. 代码示例 2
3. 少为人知的符号表2 2
3.1. 符号表 2
3.2. 代码示例 3
附:C++的59个关键词列表 3
1. 引言
这些鲜为人知的C++符号,可直接在代码中使用,但实践中不推荐这么做,可作为茶余饭后的乐趣了解C++的另一面。
虽然它们鲜为人知,但却不是GNU g++独有的,而是C++标准定义的,找一本C++标准书或pdf文档看看,就可以见到它们赫然在目。(注:C++17标准将干掉这些鲜为人知的符号)
2. 少为人知的符号表1
2.1. 符号表
代码中,可用左边的替代右边的:
可选的符号 |
可替代的常见符号 |
|
<% |
{ |
注意不能替代函数定义的{ |
%> |
} |
注意不能替代函数定义的} |
<: |
[ |
|
:> |
] |
|
%: |
# |
|
%:%: |
## |
|
and |
&& |
|
or |
|| |
|
xor |
^ |
|
bitor |
| |
|
compl |
~ |
|
bitand |
& |
|
and_eq |
&= |
|
or_eq |
|= |
|
xor_eq |
^= |
|
not |
! |
|
not_eq |
!= |
|
2.2. 代码示例
// GNU g++编译方法:g++ -g -o a a.cpp #include <stdio.h>
int main() { int N = 2;
for (int i=0; i<N; ++i) <% printf("%d\n", i); %>
return 0; } |
3. 少为人知的符号表2
三个字符等同单个字符,GNU g++编译时需要指定编译参数“-trigraphs”。注意,这个特性从C++2017标准开始已不支持。
3.1. 符号表
可选的符号 |
可替代的常见符号 |
??= |
# |
??/ |
\ |
??’ |
^ |
??( |
[ |
??) |
] |
??< |
{ |
??> |
} |
??! |
| |
??- |
~ |
3.2. 代码示例
// 注意使用GNU g++编译以下代码,需要指定编译参数“-trigraphs” // g++ -g -o b b.cpp -trigraphs #include <stdio.h>
// 下一语句完全等同于:#define index(array, i) array[i] ??=define index(array, i) array??(i??)
int main() { int m[] = { 3, 7 }; printf("m[0]=%d, m[1]=%d\n", index(m, 0), index(m, 1)); return 0; } |
附:C++的59个关键词列表
asm |
do |
if |
return |
typedef |
auto |
double |
inline |
short |
typeid |
bool |
dynamic_cast |
int |
signed |
typename |
break |
else |
long |
sizeof |
union |
case |
enum |
mutable |
static |
unsigned |
catch |
explicit |
namespace |
static_cast |
using |
char |
export |
new |
struct |
virtual |
class |
extern |
operator |
switch |
void |
const |
false |
private |
template |
volatile |
const_cast |
float |
protected |
this |
wchar_t |
continue |
for |
public |
throw |
while |
default |
friend |
register |
true |
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义