是C ++枚举已签名还是未签名? [英] Are C++ enums signed or unsigned?

查看:178
本文介绍了是C ++枚举已签名还是未签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++枚举是否已签名或未签名?通过扩展,可以安全地验证输入,通过检查它是<=您的最大值,并省略> =您的最小值(假设您从0开始并增加1)?

Are C++ enums signed or unsigned? And by extension is it safe to validate an input by checking that it is <= your max value, and leave out >= your min value (assuming you started at 0 and incremented by 1)?

推荐答案

您不应该依赖任何特定的表示。请阅读以下链接。此外,标准说,它是实现定义的哪个整数类型被用作枚举的基础类型,除了它不应该大于int,除非一些值不能适合int或无符号int。

You shouldn't rely on any specific representation. Read the following link. Also, the standard says that it is implementation-defined which integral type is used as the underlying type for an enum, except that it shall not be larger than int, unless some value cannot fit into int or an unsigned int.

简而言之:您不能依赖枚举是签名还是未签名。

In short: you cannot rely on an enum being either signed or unsigned.

这篇关于是C ++枚举已签名还是未签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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