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

如何拥有自己的私人网站平台重庆seo

如何拥有自己的私人网站平台,重庆seo,北京网站建设icp有限公司,东莞商城C语言风格枚举类型 enum 这是一个enum类型 enum LogType {OK,Info,Error };可以像这样指定enum的基础类型(默认类型是int),用英文来说就是underlying type enum LogType : unsigned int {OK,Info,Error };继续升级,指定每个枚举…

C语言风格枚举类型 enum


这是一个enum类型

enum LogType {OK,Info,Error
};

可以像这样指定enum的基础类型(默认类型是int),用英文来说就是underlying type

enum LogType : unsigned int {OK,Info,Error
};

继续升级,指定每个枚举元素具体的值

enum LogType : unsigned int {OK = 0x00,Info = 0x01,Error = 0x02
};

经典位掩码,即Bitmask写法

enum WindowFlags : unsigned int {None = 0x0000,NoMove = 0x0001,NoResize = 0x0002,NoSavedSettings = 0x0004
};
auto MySettings = NoMove | NoResize;

但是enum本质上其实是无范围枚举,即unscoped enumeration,很容易就会发现命名冲突问题

enum LogType {OK,Info,Error
}; // OKenum StatusCode {OK,Forbidden
}; // OK?auto res = LogType::OK; // Compiler doesn't know which OK to use even using :: to specify

想要解决这个问题并不难,我们可以外面套一层struct或者class

struct LogType {enum {OK,Info,Error};
};class StatusCode {enum {OK,Forbidden};
};auto res = LogType::OK; // OK!

但是这样似乎不太优雅,尤其是我们想将enum作为函数返回值的时候

struct StatusCode {enum {OK,Forbidden};
};StatusCode foobar() {return StatusCode::OK; // Hey, don't do this!
};

C++风格枚举类型 enum class

为了解决以上种种问题,C++引入了enum class,这是一种范围枚举,即scoped enumeration,不存在命名冲突
用法和enum相似,无非就是在后面添加了一个struct或者class(等效)

enum struct LogType {OK,Info,Error
};enum class StatusCode {OK,Forbidden
};

我们也可以像enum一样把enum class类型作为函数返回值(可以把它看作是一个类)

enum class StatusCode {OK,Forbidden
};StatusCode foobar() {return StatusCode::OK;
};

值得注意的是,enum class并不支持隐式类型转换,即implicit type conversion
因此,enum class也并不能像enum那样直接进行位运算

enum class WindowFlags : unsigned int {None = 0x0000,NoMove = 0x0001,NoResize = 0x0002,NoSavedSettings = 0x0004
};WindowFlags foobar() {return WindowFlags::None;
};
if (!foobar())exit(EXIT_FAILURE); // g++: expression must have bool type (or be convertible to bool)
auto MySettings = NoMove | NoResize; // g++: no operator "|" matches these operands
auto MySettings = static_cast<WindowFlags> (static_cast<unsigned int> (WindowFlags::NoMove) | static_cast<unsigned int> (WindowFlags::NoResize)); // OK

显示类型转换,即explicit type conversion似乎过于臃肿,但是我们可以重载一下enum class相关的operator,以下模板放在我们的enum class的命名空间内即可(具体细节此处不赘述,请自行查阅std::enable_if_t, std::is_enum_v)

template <typename enum_type, typename cast_type = std::size_t>
constexpr std::enable_if_t<std::is_enum_v<enum_type>, enum_type> operator& (enum_type x, enum_type y) {return static_cast<enum_type>(static_cast<cast_type> (x) & static_cast<cast_type> (y));
}template <typename enum_type, typename cast_type = std::size_t>
constexpr std::enable_if_t<std::is_enum_v<enum_type>, enum_type> operator| (enum_type x, enum_type y) {return static_cast<enum_type>(static_cast<cast_type> (x) | static_cast<cast_type> (y));
}template <typename enum_type, typename cast_type = std::size_t>
constexpr std::enable_if_t<std::is_enum_v<enum_type>, enum_type> operator^ (enum_type x, enum_type y) {return static_cast<enum_type>(static_cast<cast_type> (x) ^ static_cast<cast_type> (y));
}template <typename enum_type, typename cast_type = std::size_t>
constexpr std::enable_if_t<std::is_enum_v<enum_type>, enum_type> operator~ (enum_type x) {return static_cast <enum_type> (~static_cast<cast_type> (x));
}template <typename enum_type, typename cast_type = std::size_t>
constexpr std::enable_if_t<std::is_enum_v<enum_type>, enum_type> operator&= (enum_type &x, enum_type y) {x = x & y;return x;
}template <typename enum_type, typename cast_type = std::size_t>
constexpr std::enable_if_t<std::is_enum_v<enum_type>, enum_type> operator|= (enum_type &x, enum_type y) {x = x | y;return x;
}template <typename enum_type, typename cast_type = std::size_t>
constexpr std::enable_if_t<std::is_enum_v<enum_type>, enum_type> operator^= (enum_type &x, enum_type y) {x = x ^ y;return x;
}

enum class严格意义上来说并不是一个常规意义上的class,所以无法通过重载自动转换为bool类型,但是我们可以手写一个any函数来判断位掩码

template <typename enum_type, typename cast_type = std::size_t>
constexpr std::enable_if_t<std::is_enum_v<enum_type>, bool> any(enum_type x) {return static_cast<cast_type> (x) == 0;
};

简单样例(已载入上述模板)

enum class WindowFlags : unsigned int {None = 0x0000,NoMove = 0x0001,NoResize = 0x0002,NoSavedSettings = 0x0004
};auto MySettings = WindowFlags::NoMove | WindowFlags::NoResize;if (any(MySettings)) { // To check if any flags are set/* do something */
}

参考链接

cppreference.com

http://www.dtcms.com/wzjs/172868.html

相关文章:

  • 对中国建设银行网站的缺点朋友圈广告30元 1000次
  • 做hmtl的基本网站百度贴吧网页入口
  • 网站设计参考文献有哪些免费推广的预期效果
  • wordpress添加变量优化方案丛书官网
  • 网页视频下载软件手机版seo从零开始到精通200讲解
  • 著名的网站建设平台免费seo关键词优化排名
  • 太原哪家网站建设公司好天猫seo搜索优化
  • 东营网站建设公司惠州疫情最新情况
  • 桐城市建设局网站培训学校管理系统
  • h5 响应式手机网站淘宝店铺怎么推广和引流
  • 无锡建设网站的公司百度官方版
  • 网站项目运营方案网店运营的工作内容
  • 如何将自己做的网页做成网站考拉seo
  • 怎么做网站小编企业培训课程有哪些内容
  • wordpress 新安装 慢seo优化网站快速排名
  • 淄博网站制作定制改版西安百度代运营
  • 网站qq线客服咋做百度浏览器网页版入口
  • 网站建设 钱网址服务器查询
  • 建立免费网站的步骤免费发布平台
  • 大数据营销系统软件seo站长工具下载
  • 怎么用vscode做网站重庆森林影评
  • 那家财经网站做的好百度网页pc版登录
  • 长沙低价网站建设如何开发自己的小程序
  • 微信公众号自定义菜单wordpress网站快速排名优化
  • 电脑上不了建设厅网站金华百度seo
  • 企业b2c网站建设我想做app推广怎么做
  • 电商网站seo优化今日最新头条新闻条
  • 网站做多个产品seo关键字怎么优化
  • 设计团队网站seo技术培训泰州
  • 嘉兴网站建设哪家做得好seo排名优化哪家好