类vs枚举类作为索引类型 [英] Class vs enum class as an index type

查看:134
本文介绍了类vs枚举类作为索引类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

P0138R2提案始于 1


有一种非常有用的技术可以引入几乎是精确副本的新整数类型,现代C ++ 11程序中的不同类型:
枚举类具有明确指定的基础类型。示例:

 枚举类索引:int {}; //注意:没有枚举器。 

一个人可以将 Index 用作新的整数类型,它没有隐式转换成任何东西(好!)。


要转换 Index 为其基础类型很有用。

  int运算符*(索引索引){
返回static_cast< int>(索引);
}

创建 Index 类型是使用旧的

  class index final { 
public:
显式Index(int index = 0):index_(index){}

int运算符*()const {
返回index_;
}

私人:
int index_;
};

两者似乎基本相同,可以用相同的方式使用:

  void bar(索引索引){
std :: cout<< *指数;
}

bar(索引{1});

int i = 1;
bar(索引{i});

枚举类的优点:比较运算符是自动定义的,枚举类的con:无法指定默认构造的枚举类的索引值,它始终为零。



这些选择之间是否还有其他实际区别?





1 我将 uint32_t 更改为 int 以避免 #include< ; cstdint>

解决方案

我使用的强类型的替代方案是 NamedTypes 。他很好地解释了他博客上多个帖子中的所有详细信息,请参见 https://www.fluentcpp.com/2016/12/08/strong-types-for-strong-interfaces/


写作:使用Index = NamedType< int,struct IndexTag,Comparable,ImplicitlyConvertibleTo< int>> ;;


您需要编写类似 Index {0} 的内容,但是,当您将其用作索引时,它应该自动转换为基础类型。


它有几个优点,包括能够处理任何类型的内容。最大的缺点是它是您必须导入的外部库,而不是内置功能。<​​/ p>

P0138R2 proposal begins with1

There is an incredibly useful technique for introducing a new integer type that is almost an exact copy, yet distinct type in modern C++11 programs: an enum class with an explicitly specified underlying type. Example:

enum class Index : int { };    // Note: no enumerator.

One can use Index as a new distinct integer type, it has no implicit conversion to anything (good!).

To convert Index to its underlying type it is useful to define

int operator*(Index index) {
    return static_cast<int>(index);
}

Another way to create Index type is to use old class:

class Index final {
public:
     explicit Index(int index = 0) : index_(index) { }

     int operator*() const {
         return index_;
     }

private:  
     int index_;
};

Both seem to be largely equivalent and can be used in the same way:

void bar(Index index) {
    std::cout << *index;
}

bar(Index{1});

int i = 1;
bar(Index{i});

Pro of enum class: the comparison operators are defined automatically, con of enum class: index value for the default constructed enum class can't be specified, it is always zero.

Are there other practical differences between these alternatives?


1 I changed uint32_t to int to avoid #include <cstdint>.

解决方案

The alternative for strong types I use is a variation on NamedTypes by Jonathan Boccara. He nicely explains all of the details in multiple posts on his blog, see https://www.fluentcpp.com/2016/12/08/strong-types-for-strong-interfaces/

It's slightly more verbose in writing: using Index = NamedType<int, struct IndexTag, Comparable, ImplicitlyConvertibleTo<int>>;

When you construct this, you need to write something like Index{0}, though, the moment you use it as an index, it should automatically convert to the underlying type.

It has several advantages, including the ability to work on any type. The biggest disadvantage is that it's an external library you have to import instead of built-in functionality.

这篇关于类vs枚举类作为索引类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆