如何更改枚举(C ++)使用的整数类型? [英] How to change the integer type used by an enum (C++)?

查看:145
本文介绍了如何更改枚举(C ++)使用的整数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有C ++枚举:

If I have an C++ enum:

enum Foo
{
  Bar,
  Baz,
  Bork,
};

如何告诉编译器使用 uint16_t 实际存储枚举的值吗?

How do I tell the compiler to use a uint16_t to actually store the value of the enum?

编辑:GCC在C ++ 11的实现中是否支持此功能?

Does GCC support this feature in its implementation of C++11?

推荐答案

您不能在C ++ 98/03中执行此操作。 C ++ 11确实允许您这样做,并且没有 枚举类,其他人似乎总是告诉您:

You cannot do this in C++98/03. C++11 does allow you to do it, and without enum class the way everyone else seems to keep telling you:

enum EnumType : uint16_t
{
  Bar,
  Baz,
  Bork,
};

同样,您不必使用枚举类。并不是说这是一个坏主意,但是您不必

Again, you don't have to use enum class. Not that it's a bad idea, but you don't have to.


GCC在C ++ 11的实现中是否支持此功能?

Does GCC support this feature in its implementation of C++11?

哪个版本的GCC?看起来GCC 4.4添加了此功能,但您可能应该使用较新的版本,仅用于为了稳定。

Which version of GCC? It looks like GCC 4.4 added this functionality, but you should probably look to more recent versions, just for the sake of stability.

这篇关于如何更改枚举(C ++)使用的整数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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