C++ 在switch中使用类类型
我正在尝试在自定义类型上切换。标准上说 条件应为整型、枚举型或 将单个非显式转换函数转换为的类类型 存在整数或枚举类型(12.3)。如果情况是 类类型,则通过调用该转换来转换条件 函数,并使用转换结果代替 本节剩余部分的原始条件。完整的 进行促销活动 这表明具有一个到C++ 在switch中使用类类型,c++,visual-studio-2010,c++11,C++,Visual Studio 2010,C++11,我正在尝试在自定义类型上切换。标准上说 条件应为整型、枚举型或 将单个非显式转换函数转换为的类类型 存在整数或枚举类型(12.3)。如果情况是 类类型,则通过调用该转换来转换条件 函数,并使用转换结果代替 本节剩余部分的原始条件。完整的 进行促销活动 这表明具有一个到enum类型的隐式转换函数的类型应该是有效的开关表达式。但是当我尝试使用这个措辞时,VisualStudio给出了一个关于开关表达式是非整数的错误。VS只是在这方面不符合要求吗 类类型的定义是 struct Token {
enum
类型的隐式转换函数的类型应该是有效的开关
表达式。但是当我尝试使用这个措辞时,VisualStudio给出了一个关于开关表达式是非整数的错误。VS只是在这方面不符合要求吗
类类型的定义是
struct Token {
Token()
: line(0)
, columnbegin(0)
, columnend(0) {}
Token(const Codepoint& cp) {
*this = cp;
}
template<typename Iterator> Token(Iterator begin, Iterator end) {
columnend = 0;
columnbegin = 0;
line = 0;
while(begin != end) {
*this += *begin;
begin++;
}
}
operator TokenType() {
return type;
}
Token& operator+=(const Codepoint& cp) {
if (cp.column >= columnend)
columnend = cp.column;
if (columnbegin == 0)
columnbegin = cp.column;
Codepoints += cp.character;
if (line == 0)
line = cp.line;
return *this;
}
Token& operator=(const Codepoint& cp) {
line = cp.line;
columnbegin = cp.column;
columnend = cp.column;
Codepoints = cp.character;
return *this;
}
int line;
int columnbegin;
int columnend;
TokenType type;
string Codepoints;
};
看起来像是VC中的一个bug。GCC和4.7对此进行编译,没有问题:
enum class e { roy, gee, biv };
struct s { operator e() { return e::gee; } };
void f() {
switch ( s() ) {
case e::roy: case e::biv: case e::gee: break;
}
}
这个更简单的测试用例也能让VC开心吗?ooh有趣。。。你能发布一些伪代码吗?@AhmedMasud很清楚。。。您只需要使用可以转换为整型的对象。它在GCC上对我有用。发布一些代码?为什么你会发布很多不相关的代码而没有相关的代码?类似于使用VC++2005可以编译的东西(没有更新的)WTF是不是有这么一个糟糕的问题?!类似的代码。
enum class e { roy, gee, biv };
struct s { operator e() { return e::gee; } };
void f() {
switch ( s() ) {
case e::roy: case e::biv: case e::gee: break;
}
}