在char枚举中错误分配值 [英] Incorrect assignment of values in char enum

查看:109
本文介绍了在char枚举中错误分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用枚举,并尝试重现页面的示例。初始示例按预期工作,但是我使用以下代码获得了一些有趣的结果:

I was playing with enums and tried to reproduce some examples from this page. Initial examples worked as intended, however I got some interesting results with following code:

#include <iostream>

enum num : char {
    zero = '0',
    one = '1',
    two = '2',
    three = '3',
    four = '4',
    five = '5',
    six = '6'
};

int main()
{
    const char two = '2';
    std::cout << two << std::endl;
    std::cout << num::two;
    return 0;
}

输出是:


2

50

2
50

我预计这两个结果是一样的,但 num :: two 似乎打印了一些其他值。此值也不会更改(50),所以我认为这不是一个随机/垃圾值&有一些我不明白的char / int解析吗?以下是 ideone链接

I expected both outcomes to be the same, but the num::two seems to print some other value. Also this value doesn't changes(50), so I assume this isn't a random/garbage value & there is some sort of char/int parsing being done that I don't understand? Here is the ideone link.

我知道我可以通过分配这样的零= 0 ,而不用单引号,它可以工作。但是,我想知道幕后发生了什么,我如何控制通过单引号分配打印的非单数数字值。

I know that I can make it work by assigning like this zero = 0, without single quotes and it works. However, I want to know what is happening behind the scenes and how could I control what non-single digits value I can print via single quotes assignments.

推荐答案

这个实际上应该现在转到 char 不幸的是没有一个编译器实施 DR 1601

This should actually go to the char overload now; unfortunately none of the compilers at issue implement DR 1601.

[conv.prom] / 4


基本类型为
fixed([dcl.enum])的无范围枚举类型的pr值可以转换为其底层$ b $的prvalue b型。

A prvalue of an unscoped enumeration type whose underlying type is fixed ([dcl.enum]) can be converted to a prvalue of its underlying type.

这意味着 num 可以提升为 char


此外,如果整体升级可以应用于其底层的
类型,底层类型
固定的无范围枚举类型也可以转换为促销的底层
类型的pr值。

Moreover, if integral promotion can be applied to its underlying type, a prvalue of an unscoped enumeration type whose underlying type is fixed can also be converted to a prvalue of the promoted underlying type.

所以 num 也可以提升为 int

相关候选人是:

template <class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
                                        char ch );
template<class charT, class Traits>
basic_ostream<charT, Traits>& basic_ostream<charT, Traits>::operator<<(int);

对于这两个候选人,第一个参数是身份转换,第二个参数是促销。 num to char num to int 有促销排名。

For both candidates, the first argument is an identity conversion and the second is a promotion. Both num to char and num to int have promotion rank.

前DR1601,这些都同样好,所以模板/非模板决斗者进来。第一个是功能模板;第二个是普通成员函数,所以第二个获胜。

Pre-DR1601, these are equally as good, so the template/non-template tiebreaker comes in. The first one is a function template; the second one is a plain member function, so the second one wins.

DR1601添加了一条规则:

DR1601 added a rule that says:

一种转换,其促进底层类型为
固定为其底层类型的枚举比推广到
提升底层类型的枚举更好,如果两者不同。

A conversion that promotes an enumeration whose underlying type is fixed to its underlying type is better than one that promotes to the promoted underlying type, if the two are different.

这意味着 num to char 现在比 num int 更好,所以第一个重载现在是一个更好的匹配被选中。

This means that num to char is now better than num to int, so the first overload is now a better match and should be selected.

这篇关于在char枚举中错误分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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