谁能解释一下为什么
#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<>
不做,锡上写着什么?
如果我们查看文档有符号 http://en.cppreference.com/w/cpp/types/is_signed它说:
If T 是有符号算术类型,提供等于 true 的成员常量值。对于任何其他类型,值为 false。
并且枚举不是算术类型,因此结果应该为 false。来自 C++11 标准草案部分3.9.1
基本类型[basic.fundamental]:
[...]整型和浮点类型统称为算术
类型[...]
您可以使用以下方式获取枚举的基础类型std::underlying_type http://en.cppreference.com/w/cpp/types/underlying_type然后申请std::is_signed
到那种类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)