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

C++核心指导原则: 枚举

C++ Core Guidelines 整理目录

  1. 哲学部分
  2. 接口(Interface)部分
  3. 函数部分
  4. 类和类层次结构部分
  5. 枚举部分
  6. 资源管理部分

Enum: 枚举

Enum.1: Prefer enumerations over macros

  • 翻译: 如果需要定义一组相关的命名常量, 优先使用枚举而非宏.

  • 原因: 枚举提供了更好的类型安全性和可读性, 而宏可能会导致代码难以理解和维护.

  • 示例:

    错误示例:

    // webcolors.h (third party header)
    #define RED   0xFF0000
    #define GREEN 0x00FF00
    #define BLUE  0x0000FF
    
    // productinfo.h
    // The following define product subtypes based on color
    #define RED    0
    #define PURPLE 1
    #define BLUE   2
    
    int webby = BLUE;   // webby == 2; probably not what was desired
    

    正确做法:

    enum class Web_color { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF };
    enum class Product_info { red = 0, purple = 1, blue = 2 };
    
    int webby = blue;   // error: be specific
    Web_color webby = Web_color::blue;
    

    enum class可以规避命名冲突.

Enum.2: Use enumerations to represent sets of related named constants

  • 翻译: 使用枚举来表示相关命名常量的集合.

  • 原因: 这样可以增加代码的可读性和类型安全性, 使代码更易于理解和维护.

  • 示例:

    enum class Product_info { red = 0, purple = 1, blue = 2 };
    
    void print(Product_info inf) {
      switch (inf) {
        case Product_info::red: cout << "red"; break;
        case Product_info::purple: cout << "purple"; break;
      }
    }
    

    对于 switch case 没有提到的枚举, 编译器通常会有警告.

Enum.3: Prefer enum classes over “plain” enums

  • 翻译: 优先选择枚举类(enum class)而非普通枚举(plain enum).
  • 原因: 枚举类提供了更强的类型安全和作用域控制, 避免了命名冲突和意外的类型转换.

Enum.4: Define operations on enumerations for safe and simple use

  • 翻译: 为枚举定义操作, 以确保它们的安全使用和简化代码.
  • 原因: 适当的枚举操作可以提高代码的健壮性和可读性, 减少错误的发生.

Enum.5: Don’t use ALL_CAPS for enumerators

  • 翻译: 不要对枚举成员使用全大写字母.

  • 原因: 这有助于区分枚举成员和其他常量, 提高代码的可读性. 宏通常使用全大写字母来表示常量.

  • 示例:

      // webcolors.h (third party header)
    #define RED   0xFF0000
    #define GREEN 0x00FF00
    #define BLUE  0x0000FF
    
    // productinfo.h
    // The following define product subtypes based on color
    
    enum class Product_info { RED, PURPLE, BLUE };   // syntax error
    

Enum.6: Avoid unnamed enumerations

  • 翻译: 避免使用未命名的枚举.

  • 原因: 未命名的枚举可能会导致代码维护上的困难, 降低代码的可读性和可维护性.

  • 示例:

    错误示例:

    enum { red = 0xFF0000, scale = 4, is_signed = 1 };
    

    这种情况可能是你错误的把一些不相干的常量放到一起. 可以考虑用constexpr来重写这些常量.

Enum.7: Specify the underlying type of an enumeration only when necessary

  • 翻译: 仅在必要时指定枚举的基础类型.
  • 原因: 通常情况下编译器会自动选择合适的基础类型, 手动指定可能会影响代码的可移植性和可维护性.
  • 示例:
// 枚举值比较小时可以用较小的类型来存储, 节省空间
enum class Direction : char { n, s, e, w,
                              ne, nw, se, sw };

// 声明为int32_t是多余的
enum class Web_color : int32_t { red   = 0xFF0000,
                                  green = 0x00FF00,
                                  blue  = 0x0000FF };

Enum.8: Specify enumerator values only when necessary

  • 翻译: 仅为枚举成员指定值在必要时.
  • 原因: 默认情况下编译器会自动为枚举成员分配连续的整数值, 手动指定值可能会引入不必要的复杂性.

http://www.kler.cn/a/559149.html

相关文章:

  • NIO-Reactor模型梳理与demo实现
  • 单页图床HTML源码+本地API接口图床系统修复版源码
  • SpringBoot整合Redis和Redision锁
  • Linux 常见面试题汇总
  • Qt QStackedWidget 总结
  • 前后端对接
  • 大数据迁移时业务应用有哪些可能的变更
  • 【Redis】基础知识入门
  • Linux CentOS 上 Ollama 的安装与部署:从入门到实践
  • Ae 效果详解:3D 通道提取
  • Sliding Window Attention(滑动窗口注意力)解析: Pytorch实现并结合全局注意力(Global Attention )
  • CSS—盒模型(3分钟结合示例精通盒模型)
  • windows使用命令解压jar包,替换里面的文件。并重新打包成jar包,解决Failed to get nested archive for entry
  • Python 基本语法的详细解释
  • 深度解析:大模型在多显卡服务器下的通信机制与分布式训练——以DeepSeek、Ollama和vLLM为例
  • vue前端菜单权限控制
  • 【Java 面试 八股文】JVM 虚拟机篇
  • 谷歌浏览器更新后导致的刷新数据无法显示
  • 给小米/红米手机root(工具基本为官方工具)——KernelSU篇
  • Pytorch使用手册-音频数据增强(专题二十)