【发布时间】:2018-04-07 11:33:39
【问题描述】:
我有一个快速的代码片段:
#include<iostream>
using namespace std;
enum class A:int {
u=1,
v=2,
w=3
};
template<A value>
int nextEnum(){
return value+1;
}
int main() {
nextEnum<A::u>();
return 0;
}
编译失败:
clang++ testenum.cpp -std=c++11
testenum.cpp:10:17: error: invalid operands to binary expression ('A' and 'int')
return value+1;
好吧,我已经将 A 声明为“int”枚举,为什么它不能用 int 来“+”?如何解决?
【问题讨论】:
-
枚举的“继承”更多是针对范围和大小,而不是针对实际继承。此外,通过使用scoped enumerations,类型名称
A是强类型。它不能隐式转换为其他整数类型,您必须显式地进行转换。