ENUM常数问题 [英] ENUM constants questions

查看:122
本文介绍了ENUM常数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本网站上发布了如何通过执行以下操作来生成唯一的枚举常量:

 枚举_EXAMPLE 
{
LEFT ='left',
RIGHT ='right'
// etc
};

忽略有效性问题,数字如何生成?更具体地说,使用什么技术?我想尝试构建一个模拟短字符串的函数。

解决方案

'left' 是一个多字符文字(C ++ 03的2.13.2 / 1),它有类型 int 。 / p>

它实现定义了它实际具有的整数值。特别是,不保证'left''right'不相等,所以你在在一个枚举中使用它们的一些失败者。



但是,GCC在此举例说明其行为: http://gcc.gnu.org/onlinedocs/gcc-4.6.1/cpp/Implementation_002ddefined -behavior.html#Implement_002ddefined-behavior



'right'有五个字符,显然是每5个字符的字符串不能有不同的32位值。


It was posted on this site how you could generate unique enum constants by doing the following:

enum _EXAMPLE
{
    LEFT = 'left',
    RIGHT = 'right'
    //etc
};

Ignoring the issue of validity, how are the numbers generated? More specifically, what technique is used? As I wish to try to build a function that emulates it for short strings.

解决方案

'left' is a multicharacter literal (2.13.2/1 of C++03), it has type int.

It's implementation-defined what integer value it actually has. In particular, there's no guarantee that 'left' and 'right' aren't equal, so you're on to something of a loser using them in an enum.

For an example, though, GCC documents its behavior here: http://gcc.gnu.org/onlinedocs/gcc-4.6.1/cpp/Implementation_002ddefined-behavior.html#Implementation_002ddefined-behavior

'right' has five characters, and clearly it's not possible for every 5-character string to have a different 32 bit value.

这篇关于ENUM常数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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