【发布时间】:2017-06-01 13:24:39
【问题描述】:
在下面的代码中,我使用static_cast 将强类型的enum 转换为int。在另一个方向上也是如此。但如果演员表int 不在enum 的范围内,它也可以工作。为什么会这样,为什么编译器没有捕捉到这个?
#include <iostream>
#include <string>
enum class Name {Hans, Peter, Georg}; // 0, 1, 2
std::string getName(Name name) {
switch(name) {
case Name::Hans: return "Hans";
case Name::Peter: return "Peter";
case Name::Georg: return "Georg";
default: return "not valid name";
}
}
int main()
{
// Cast a Name to an int, works fine.
std::cout<< static_cast<int>( Name::Peter ) <<std::endl; // 1
std::cout<< static_cast<int>( Name::Hans ) <<std::endl; // 0
// Cast an int to a Name
std::cout<< getName(static_cast<Name>(2)) <<std::endl; // Georg
std::cout<< getName(static_cast<Name>(3)) <<std::endl; // not a valid name
// I would expect a compiler error/warning like i get here:
// std::cout<< static_cast<int>( Name::Hans + 4 ) <<std::endl;
}
【问题讨论】:
-
你认为编译器应该在什么基础上“捕捉”这个?据我所知,C++ 标准不需要对此进行任何诊断。
-
添加整数值有效性检查将产生运行时开销。
-
当我取消注释最后一行时我得到的错误是
no match for ‘operator+’ (operand types are ‘Name’ and ‘int’),这与你关于枚举范围的问题没有任何关系。 -
我希望可以选择这个,但是,clang 和 gcc 都不会为此发出警告。当您使用
static_cast时,您已经告诉编译器:我知道得更好! -
转换意味着通过提供它(类型系统)不能或不允许自己推断的附加信息来覆盖类型系统。编译器将隐含地相信此信息是准确的,无论它多么不可信。强制转换覆盖了一个安全系统,它们不是隐式安全的,正确使用它们的责任落在了开发者身上。
标签: c++ enums casting compiler-errors