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

C++ 语言特性13 - 强枚举类型

一:概述

        C++11 引入了强枚举类型,这种新的枚举类型,提供了比传统枚举类型更强的类型安全性检查,与传统枚举类型相比,主要有以下区别:

1. 传统枚举类型(C++98):

  1. 类型安全性:C语言中的枚举不会提供类型安全。枚举值可以隐式转换为整数,也可以从整数隐式转换为枚举类型,这可能导致类型安全问题。

  2. 作用域:C语言中的枚举成员是公开的,这意味着它们在枚举类型定义的作用域内是可见的,并且可以不加区分地访问。

  3. 底层类型:枚举成员默认的底层类型是整数(通常是int),但C语言不允许显式指定底层类型。

  4. 赋值和比较:枚举类型可以与整数进行赋值和比较操作,枚举值之间也可以直接比较。

  5. 转换:枚举值可以隐式转换为整数,整数也可以隐式转换为枚举类型。

2. 新的强枚举类型(C++11):

  1. 类型安全性:C++中的强枚举提供了更好的类型安全。枚举类成员不能隐式转换为整数或其他类型,必须使用显式类型转换。

  2. 作用域:强枚举的成员被限定在枚举类型的作用域内,必须使用枚举类型名称和作用域解析运算符(::)来访问。

  3. 底层类型:可以为强枚举显式指定底层类型,如uint8_tint32_t等,提供了更多的灵活性。

  4. 赋值和比较:强枚举成员不能直接与整数进行赋值和比较操作,必须进行显式类型转换。

  5. 转换:强枚举类型与整数之间的转换需要显式进行,不能隐式转换。

3. 举例

//传统枚举类型
enum Color {
    RED, GREEN, BLUE
};

int main() {
    enum Color color = RED;
    int num = color; // 隐式转换
    color = 2; // 隐式转换
    return 0;
}
enum class Color {
    RED, GREEN, BLUE
};

int main() {
    Color color = Color::RED;
    int num = static_cast<int>(color); // 显式转换
    // color = 2; // 错误:不能隐式转换
    return 0;
}

二:强枚举类型的使用场景 

1. 用于表示状态,类别,标志位等

//表示状态
enum class Color {
    Red,
    Green,
    Blue
};

void setLightColor(Color color) {
    // ...
}

int main() {
    setLightColor(Color::Red); // 正确
    // setLightColor(3); // 错误:不能隐式转换为Color类型
}

//------------------------------------------------------
//表示类别
enum class Direction {
    Up,
    Down,
    Left,
    Right
};

Direction turnDirection = Direction::Right;
// int direction = turnDirection; // 错误:需要显式转换

//------------------------------------------------------
//表示错误码
enum class ErrorCode {
    Ok,
    NotFound,
    PermissionDenied,
    InvalidArgument
};

ErrorCode readFile(const std::string& filename) {
    // ...
    return ErrorCode::NotFound;
}
//------------------------------------------------------
//表示标志位
enum class AccessFlags : unsigned int {
    Read = 1 << 0,
    Write = 1 << 1,
    Execute = 1 << 2
};

AccessFlags flags = AccessFlags::Read | AccessFlags::Write;


2.  强枚举类型的枚举值在编译时是已知的,可以利用这一特性用在模板编程中,比如模板特化处理,模板元编程,模板条件编译等

//1. 模板特化
template <typename T>
struct Processor;

template <>
struct Processor<Color> {
    void process(Color color) {
        // 特定于颜色的处理
    }
};

int main() {
    Processor<Color> processor;
    processor.process(Color::RED);
    return 0;
}

/********************************************/
//2. 模板元编程
template <Color color>
struct ColorTraits {
    static const char* name = "Unknown";
};

template <>
struct ColorTraits<Color::RED> {
    static const char* name = "Red";
};

template <>
struct ColorTraits<Color::GREEN> {
    static const char* name = "Green";
};

// ...

/************************************************/
//3. 条件编译
template <LogLevel level>
void logMessage(const std::string& message) {
    if (level == LogLevel::Info) {
        // 记录信息级别的消息
    } else if (level == LogLevel::Fatal) {
        // 记录致命错误并退出
        std::cerr << "Fatal error: " << message << std::endl;
        std::exit(1);
    }
}

int main() {
    logMessage<LogLevel::Info>("This is an informational message.");
    return 0;
}

 


http://www.kler.cn/news/331562.html

相关文章:

  • IP 数据包分包组包
  • mit6824-01-MapReduce详解
  • 解决 TypeError: Expected state_dict to be dict-like, got <class ‘*‘>.
  • 在 Ubuntu 下通过 Docker 部署 NAS 服务器
  • 损失函数篇 | YOLOv5 引入Unified-IoU 高质量目标检测IoU损失
  • Vue3项目开发——新闻发布管理系统(九)(完结篇)
  • 项目-坦克大战学习-资源冲突解决
  • 算法 | 鹈鹕算法POA-Transformer-LSTM多变量回归预测
  • redis 5的安装及启动(window)
  • csapp_计算机系统通览
  • 数据校验的总结
  • 《开源大模型食用指南》,一杯奶茶速通大模型!新增Examples最佳实践!
  • 【pytorch】pytorch入门5:最大池化层(Pooling layers )
  • SSY20241002提高组T4题解__纯数论
  • nginx配置多域名共用服务器80端口
  • ICM20948 DMP代码详解(60)
  • SpringBoot技术在墙绘艺术市场的应用
  • 在VS code 中部署C#和avalonia开发环境
  • 【Qt Quick】基础语法:变量类型
  • Vue-Lecture1-Notes