问题标签 [enum-class]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
8744 浏览

c++ - switch 语句中从 int 到 enum 类的隐式转换

上面的代码片段在 msvc2012 中编译得很好(并且可以工作),但在 clang-3.4 和 g++-4.8 中失败。这些需要static_cast<pid>(propId)在 switch 子句中使用。

顺便说一句,没有显式转换的简单赋值,例如pid a = propId;在每个编译器中都会产生错误。

哪一个做对了?

0 投票
1 回答
1276 浏览

c++ - Initialization of a static constexpr class member of enum-class type by explicit conversion function

I have a discrepancy between the behaviour of g++ 4.8.1 and clang++ 3.4.

I've got a class A, of literal type, that has an explicit constexpr conversion function to type enum class E.

Gcc allows me to initialize constexpr variables of type E from a constant expression of type A using the conversion function in some cases, but not when the variable is a static class member (e2 below)

Clang rejects the initialization in all contexts (e1, e2 and e3).

According to [over.match.conv]p1 use of an explicit conversion function is OK here

I see a similar pattern when converting to another literal class type instead of an enum type - g++ rejects the initialization of s1, clang rejects the initialization of s1, s2 and s3. I think these should be valid as well, as per [over.match.copy]p1.

Which compiler, if either, is right?


Edit: A couple of interesting things to note:

  1. The results are different between clang-3.4 and clang-svn, see comments below.
  2. When using parens for the initialization instead of braces, there is still a difference between e2/s2 and e1/e3/s1/s3, see http://coliru.stacked-crooked.com/a/daca396a63425c6b. gcc and clang-svn agree, but I'm not convinced that rejecting e2 and s2 is correct.
0 投票
2 回答
1100 浏览

c++ - 为什么枚举不能是模板?

enumeration cannot be a template是我尝试使用BCC64(基于 Clang)编译以下代码时给出的错误:

起初,我认为这个明确的禁止是由于enum底层类型限制,如果枚举底层类型可以被模板化,那么它可能导致格式错误的枚举,但是当我们尝试这个时:

只要A类型遵循与枚举基础类型相同的限制,它就可以毫无问题地编译,你知道,定义枚举值的表达式:

  • 应该是一个足够大的整数常量以适合枚举的所有值
  • 每个枚举类型都应兼容charsigned/unsigned整数类型。

如果我们不遵守此规则,(使用类内或全局枚举)会出现另一个特定错误,如预期的那样:

非整数类型 'fooClass' 是无效的基础类型

所以,这就是为什么我想知道为什么即使已经对底层类型进行了控制,为什么也明确禁止创建模板枚举。标准中哪里提到了这个禁令?

感谢您的关注。

0 投票
2 回答
4104 浏览

c++11 - QSettings 中 QVariant 中的枚举类

我对枚举类、QVariants 和 QSettings 类有疑问。我想将枚举类值存储在 QVariant 中,该 QVariant 进入 QSettings 实例。所以,我的代码实际上看起来像这样:

在执行代码的这一点上,一个断言跳进来并抱怨:

在互联网上搜索,我发现该类缺少合适的 << 和 >> 运算符。但这不是枚举类的选择。我什至尝试使用

但它没有帮助。也许您对我有其他建议/解决方案。谢谢!

0 投票
2 回答
1095 浏览

c++ - 是否可以使范围枚举(“枚举类”)在上下文中可转换为布尔值?

假设我有

是否有可能使枚举类在上下文中可转换为 bool 以允许某人做

0 投票
6 回答
8897 浏览

c++ - 为什么不能通过指针将 C++11 强类型枚举转换为基础类型?

在 C++11 中,我们可以将强类型枚举 ( enum class) 转换为其底层类型。但似乎我们不能将指针转换为相同的:

我试图理解为什么会这样:枚举机制是否有一些东西使得支持这一点变得困难或荒谬?这是标准中的简单疏忽吗?还有什么?

在我看来,如果枚举类型真的建立在上面的整数类型之上,我们应该不仅可以转换值,还可以转换指针。我们仍然可以使用reinterpret_cast<int*>C 风格的铸件,但这比我认为我们需要的要大。

0 投票
3 回答
14221 浏览

c++ - Qt - 具有枚举类类型的 Q_DECLARE_METATYPE()

有没有办法将 Q_DECLARE_METATYPE() 与枚举类类型一起使用?我知道旧的枚举可以工作,但是这些新的、强类型的枚举呢?在其他地方找不到有关此问题的任何信息。我正在使用可用的最新 Qt 版本。

例子:

0 投票
0 回答
165 浏览

c++ - Astyle C++ 枚举类缩进

有没有办法让 Astyle 将枚举类元素缩进到相同的深度?

我得到以下信息:

而我想要类似的东西:

大枚举向右缩进太远。

当前选项为 kr 样式,选项:SLC N。

0 投票
0 回答
50 浏览

c++ - 为什么枚举类不允许使用 var.constant?

假设我们有一个枚举类:

要引用 E 中的枚举数,我们可以写成E::constant,但下面是非法的:

但是考虑一下:

两者都是合法的S::constants.constant允许e.constant枚举类不是更一致吗?无能是故意的吗?

0 投票
3 回答
7241 浏览

c++ - c ++:使用“枚举类”在类中枚举

在类中编写枚举的正确方法是什么?我正在用 C++ 编写康威的生命游戏代码。所以我有一个模式类,其中包含有关不同类型模式的信息:

我的目标是不污染这里的全球空间。因此,它是在类中编写枚举的编写方式,同时牢记 oops 概念。如果没有,请提出一些更好的替代方案,以及它们比这种方法有什么好处。欢迎任何关于在课堂内编写枚举时记住事情的意见。

编辑:@Jay Miller 根据他的意见,我已经尝试了他所有的三种方法

我的发现(在尝试了代码和谷歌搜索之后):

  1. 在我的案例中,PatternSimple 是在类内部编写枚举的最简单方法;但它在类型安全上妥协,即我可以将枚举值与任何其他数据类型进行比较,它不会给出任何错误。
  2. PatternClassIn 提供类型安全;但我必须在调用函数中编写更多代码才能访问枚举值。
  3. PatternClassOut 提供了两全其美。它提供类型安全,此外,调用函数访问枚举值的代码与 PatternSimple 的代码相同。