为什么在枚举名称中的字符数量之前先枚举打字机
Why enumeration typenames are preceded with the number of characters in the enumeration name?
考虑以下代码:
#include <iostream>
#include <typeinfo>
enum class Colors
{
Red,
Blue,
Green
};
int main ()
{
std::cout << typeid(Colors::Red).name();
return 0;
}
上述程序的输出为6Colors
。即使枚举"未划分"(没有class
关键字的枚举),也是如此。Animals
定义了另一个枚举时,其类型名称将变为7Animals
。尽管这在将来可能几乎没有考虑,但我很想知道编译器为什么这样做。
您正在看到一个杂乱无章的名称 - 该名称旨在编码名称范围和键入信息以及名称。
Boost具有方便的跨平台方法,可以删除这些名称,您可能会发现有趣的玩法:
#include <iostream>
#include <typeinfo>
#include <boost/core/demangle.hpp>
enum class Colors
{
Red,
Blue,
Green
};
int main ()
{
std::cout << boost::core::demangle(typeid(Colors::Red).name());
return 0;
}
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- 访问在 C++ 结构中声明的枚举变量
- 枚举类'classname'的多重定义
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- typedef 枚举和枚举类有什么区别?
- 为什么我的开关/机箱在使用枚举时默认?
- 标准::可选枚举的比较运算符
- C++两个源文件之间共享的枚举的静态实例
- 打印没有铸件的枚举可以在C++中吗?
- 枚举成员与静态 int 成员?
- C++:枚举:错误:应使用标识符而不是"}"
- 带有 c++ 的枚举(输入检查)
- 在 qml 中使用 Q_ENUM 和 Q_PROPERTY 作为枚举类
- 为什么 int 类型的枚举类值不能用作 int
- 为什么在枚举名称中的字符数量之前先枚举打字机