C ++枚举是否签名或无符号? [英] Are C++ enums signed or unsigned?

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

问题描述

C ++枚举是否签名或无符号?另外,通过检查输入是否是<=你的最大值,并忽略> =你的最小值(假设你从0开始并递增1)就可以安全地验证输入?

解决方案

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



简而言之:您不能依赖枚举被签名或无符号。


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)?

解决方案

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天全站免登陆