std :: is_signed不适用于强类型枚举:int [英] std::is_signed does not work for strongly typed enums : int

查看:157
本文介绍了std :: is_signed不适用于强类型枚举:int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么

Can anyone please explain, why

#include <iostream>
#include <type_traits>
using namespace std;
enum E : signed int { a=-1, b = 1,};
int main() {
    std::cout << std::boolalpha;
    cout << "strong typed enum E:int should be signed, but is_signed returns " 
    << is_signed<E>() << "\n";
    return 0;
}

std :: is_signed<>不做,在锡上说什么?
谢谢...

std::is_signed<> does not do, what it says on the tin? Thanks...

推荐答案

如果我们查看 is_signed 它说:


如果 T是带符号的算术类型,提供成员常数值等于true。对于任何其他类型,值为false。

If T is a signed arithmetic type, provides the member constant value equal true. For any other type, value is false.

,枚举不是算术类型,所以结果应该是false。从C ++ 11标准部分草案 3.9.1 基本类型[basic.fundamental]:

and an enum is not an arithmetic type so the result should be false. From the draft C++11 standard section 3.9.1 Fundamental types [basic.fundamental]:


[...]积分和浮动类型统称为算术
类型[...]

[...]Integral and floating types are collectively called arithmetic types[...]

您可以使用 std ::底层类型获取枚举的基础类型,然后将 std :: is_signed 应用于该类型。

You can get the underlying type for an enum using std::underlying_type and then apply std::is_signed to that type.

这篇关于std :: is_signed不适用于强类型枚举:int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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