使用switch中的类类型
Using class types in switch
我试图在自定义类型上switch
。
条件必须是整型、枚举型或a为其提供单个非显式转换函数的类类型存在整型或枚举类型(12.3)。如果条件是类类型时,通过调用该转换来转换条件函数,并使用转换的结果代替本节其余部分的原始条件。积分执行提升。
这表明一个类型如果有一个隐式转换函数到enum
类型,那么它应该是一个有效的switch
表达式。但是当我尝试使用这种措辞时,Visual Studio给出了一个关于开关表达式是非积分的错误。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;
};
以switch(*begin)
为错误线,其中begin
是vector<Token>::iterator
。
std::vector<Token>::iterator begin = vector.begin();
switch(*begin) {
case TokenType::stuff:
}
看起来像VC中的一个bug。GCC 4.5和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高兴吗?
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 通过switch和static_cast访问多态对象的运行时类型
- 如何在 c++ 中从 switch 语句返回不同类型的变量
- Switch语句不显示除默认语句之外的任何类型的输出
- 用于初始化派生类型的泛型 switch 语句
- 使用switch中的类类型
- c++ (Switch语句类型错误)
- 为什么在switch case语句中只允许有限的类型?