当前位置: 首页 > news >正文

C++标识符:检查是否和保留字冲突

1. 基础知识

最基本的要求: 字母、数字、下划线组成, 并且不能是数字开头。

禁忌1: C++ 关键字不能用做标识符。 它们是:

alignas alignof asm auto
bool break
case catch char char16_t char32_t class const constexpr const_cast continue
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 nullptr
operator
private protected public
register reinterpret_cast 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

禁忌2:不能包含两个连续的下划线。

禁忌3:不能以下划线 + 大写字母开头。

禁忌4:函数外面定义的标识符, 不能以下划线开头。

禁忌5:宏定义也是 identifier,也需要遵循前面提到的要求: https://en.cppreference.com/w/cpp/preprocessor/replace

禁忌6:和已有宏定义同名, 例如 #include <windows.h> 后, 默认会引入 small/min/max 等宏定义, 会导致无法使用它们作为变量名字(编译报错).

2. 规则太多,记不住?

用 clang 编译器。 指定 -Werror=reserved-identifier 编译选项。

给一个富含各种 reserved identifier 的内容

#define _ENABLE_TEST 1

int _Count;

int _monster;

int main()
{
    int hello__world;
    int _Kali;

    int _yes;

    return 0;
}

这份代码中只有 _yes 是合法 identifier, 其他都和 reserved identifier 冲突了。

编译:检测到了每一处非法的identifier 命名

g++ test39.cpp -std=c++11 -Werror=reserved-identifier

在这里插入图片描述

相关文章:

  • SQL 语句说明
  • 从竞速到巡检:不同无人机如何匹配最佳PCB方案?
  • 基于若依和elementui实现文件上传(导入Excel表)
  • Cygwin中使用其它平台生成的动态库
  • 【连载3】基础智能体的进展与挑战综述
  • Day11:关于MySQL的数据查询——子查询和联合查询
  • Python语言介绍
  • 代码随想录算法训练营day2(数组)
  • Docker安装常用软件教程(带完整命令)
  • 6.好事多磨 -- UDP网络连接
  • STP基本实验
  • 【C语言】预处理(下)(C语言完结篇)
  • 事件过滤器
  • MySQL 面经
  • 十七、TCP编程
  • 在 Lua 中实现 JSON 与 Table 的相互转换的详细使用方法
  • 《人件》第二章 办公环境
  • CLIP中的Zero-Shot Learning原理
  • `offset_mapping` 是什么
  • C语言—程序的编译和链接
  • 网站制作论坛/百度做网站
  • 西宁网站网站建设/重庆seo网站
  • 类似5173的网站怎么做/青岛网站推广企业
  • 网站 被攻击_主业篡改 被黑了 织梦做的站/百度自动优化
  • 要想学做网站/东莞seo排名优化
  • 做网站花了2万多/批量外链工具