2017-06-13 28 views
7

Đoạn mã sau biên dịch tốt trên tất cả các trình biên dịch mà tôi đã kiểm tra (clang, mingw, g ++) ngoài MSVC.Tại sao là == toán tử quá tải của enum mơ hồ trong 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 không thành công với các lỗi sau:

>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)' 

Bất kỳ cái nhìn sâu sắc sẽ là tuyệt vời, tôi đã gãi đầu của tôi về vấn đề này cả ngày.

Phiên bản MSVC của tôi là 14.0 tuy nhiên tôi đã thử nghiệm trực tuyến với phiên bản Phiên bản 19.00.23506 và cùng một lỗi xuất hiện.

Lỗi này không mặc nhiên với phiên bản 19.11.25331.0. Lỗi trình biên dịch?

+6

Có thể vì có tích hợp sẵn. – StoryTeller

+1

Là một lưu ý phụ, tôi sẽ nhầm lẫn nếu tôi phải sử dụng phiên bản 'operator ==' của bạn vì nó không kiểm tra sự bình đẳng. – piwi

+0

@piwi - nó chỉ là mã tối thiểu để tái tạo lỗi mơ hồ, – hippiemancam

Trả lời

7

Đối với liệt kê, có toán tử so sánh được tích hợp sẵn. Khi bạn xác định máy của bạn, tính năng tích hợp sẵn được ẩn tự động.

[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 ]

Để trả lời câu hỏi của bạn, vâng, nó có vẻ như một lỗi biên dịch.

+0

Cảm ơn vì điều đó, thật tốt khi biết rằng tôi không chỉ phát điên. – hippiemancam

+0

Đó không phải là cách ứng viên tích hợp hoạt động. –

+0

@ T.C. - Các đoạn văn được trích dẫn (hiện đã được sửa), nhưng dù sao thì lỗi trình biên dịch. – StoryTeller