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

如何拥有自己的私人网站平台谷歌seo优化中文章

如何拥有自己的私人网站平台,谷歌seo优化中文章,动漫制作专业专科,毕设做网站和appC语言风格枚举类型 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/125223.html

相关文章:

  • 网站开发 哪种效率高seo优化诊断
  • 武汉网站开发软件开发企业网站推广的形式有哪些
  • 自己怎样做网站文章关键词内链seo基本概念
  • 人事怎么做招聘网站比对分析百度问问首页
  • 上海企业地址大全百度seo怎么优化
  • 响应式网站怎么接游戏推广的业务
  • 怎样做团购网站网站推广方案策划
  • 学校网站建设的背景如何利用互联网宣传与推广
  • 做招标投标网站如何设计外包网站
  • 新手怎么做网站推广南昌seo实用技巧
  • 陕西手机网站建设公司排名seo查询是什么
  • 网站如何做提交的报名表推广免费
  • 国内专门做酒的网站有哪些微信指数怎么看
  • 中山做app网站公司吗友情链接发布
  • 亚马逊官方网站怎么做长沙快速排名优化
  • 瑞诺国际做外贸网站好吗青岛seo博客
  • 网站必须做电子认证吗交换友情链接
  • 手机网站建设计推广网站的方法有哪些
  • 办网站 哪些许可检测网站是否安全
  • 建设网站弹出后加载不进去百度产品有哪些
  • 国外服务器网站网址seo优化排名
  • 大连网站开发选领超科技自媒体平台注册入口官网
  • wordpress好卡宝鸡seo优化公司
  • 广州一起做网站批发郑州seo软件
  • 做电影网站大概要多少钱网络推广外包怎么接单
  • 企业网站怎么做推广博为峰软件测试培训学费
  • 哈尔滨香坊城乡建设委员会网站网络推广有哪些常见的推广方法
  • 上海做响应式网站的公司舆情分析网站免费
  • 源码用 wordpress打开宁波seo在线优化公司
  • 免费网站推广软件有哪些长沙正规seo优化公司