g++ 4.9.0接受以下代码:
enum E { foo };
struct C {
operator E() const { return foo; }
operator E() { return foo; }
};
int main() {
C c;
switch (c) {
case foo: break;
}
}
但是clang 3.4.1会拒绝它,并显示以下诊断信息:
12 : error: multiple conversions from switch condition type 'C' to an integral or enumeration type
switch (c)
^ ~
5 : note: conversion to enumeration type 'E'
operator E() const { return foo; }
^
6 : note: conversion to enumeration type 'E'
operator E() { return foo; }
^
哪一个是正确的?这是clang的bug,g++的bug,libstdc++的bug,标准缺陷还是其他问题?我做了什么愚蠢的事情吗?
在引发这个问题的代码中,C是std::atomic,而std::atomic::operator T被重载为const和const volatile的cv限定符。
两个编译器都接受E e = c;,所以看起来与switch语句有关。