2017-06-13 23 views
7

Poniższy kod kompiluje dobrze na wszystkich kompilatorów, które sprawdziłem (clang, mingw, g ++) innych niż MSVC.Dlaczego == operator przeciążenie enum dwuznaczne w MSVC

enum class Foo{BAR}; 

bool operator==(Foo a, Foo b) 
{ 
    return (int)a & (int)b; 
} 

int main(int argc, char *argv[]) 
{ 
    Foo::BAR==Foo::BAR; 
    return 0; 
} 

MSVC nie powiedzie się z powodu następującego błędu:

>main.cpp(10): error C2593: 'operator ==' is ambiguous 
>main.cpp(3): note: could be 'bool operator ==(Foo,Foo)' 
>main.cpp(10): note: while trying to match the argument list '(Foo, Foo)' 

Każdy wgląd byłoby świetnie, byłem drapiąc głowę o tym cały dzień.

Moja wersja MSVC to 14,0 jednak testowałem ją online w wersji 19.00.23506 i pojawia się ten sam błąd.

Błąd nie występuje jednak w wersji 19.11.25331.0. Błąd kompilatora?

+6

Prawdopodobnie dlatego, że jest wbudowany. – StoryTeller

+1

Na marginesie, byłbym zdezorientowany, gdybym musiał użyć twojej wersji 'operatora ==' ponieważ nie testuje równości. – piwi

+0

@piwi - to tylko minimalny kod do odtworzenia niejednoznacznego błędu, – hippiemancam

Odpowiedz

7

Dla wyliczeń istnieje wbudowany operator porównania. Kiedy definiujesz swoje, wbudowane ma być ukryte automatycznie.

[over.built/1]

The candidate operator functions that represent the built-in operators defined in Clause [expr] are specified in this subclause. These candidate functions participate in the operator overload resolution process as described in [over.match.oper] and are used for no other purpose. [ Note: Because built-in operators take only operands with non-class type, and operator overload resolution occurs only when an operand expression originally has class or enumeration type, operator overload resolution can resolve to a built-in operator only when an operand has a class type that has a user-defined conversion to a non-class type appropriate for the operator, or when an operand has an enumeration type that can be converted to a type appropriate for the operator. Also note that some of the candidate operator functions given in this subclause are more permissive than the built-in operators themselves. As described in [over.match.oper], after a built-in operator is selected by overload resolution the expression is subject to the requirements for the built-in operator given in Clause [expr], and therefore to any additional semantic constraints given there. If there is a user-written candidate with the same name and parameter types as a built-in candidate operator function, the built-in operator function is hidden and is not included in the set of candidate functions. — end note ]

Aby odpowiedzieć na to pytanie, tak, wydaje się błędem kompilatora.

+0

Dzięki temu, dobrze jest wiedzieć, że nie szaleję. – hippiemancam

+0

To nie jest tak, jak działają wbudowani kandydaci. –

+0

@ T.C. - Złe akapity cytowane (teraz naprawione), ale mimo to błąd kompilatora. – StoryTeller

Powiązane problemy