2017-06-13 25 views
7

içinde belirsiz enum operatör aşırı Aşağıdaki kod MSVC dışındaki ı işaretlediğiniz tüm derleyiciler (çınlama, mingw, g ++) ince derler olduğunu.Neden == 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 aşağıdaki hata nedeniyle başarısız:

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

Herhangi bir fikir çok iyi olurdu, bütün gün bu konuda kafamı çizilmeye oldum.

MSVC sürümüm 14.0, ancak 19.00.23506 numaralı sürümüyle çevrimiçi olarak test ettim ve aynı hata görüntüleniyor.

hata ancak sürümü 19.11.25331.0 ile apear etmez. Derleyici hata o zaman? numaralandırma için

+6

Muhtemelen yerleşik olanı olduğu için. – StoryTeller

+1

Bir yan not olarak, '== = sürümünüzü kullanmak zorunda kaldığımda, eşitliği sınamadığı için kafam karışırdı. – piwi

+0

@piwi - sadece belirsiz bir hatayı yeniden üretmek için gereken minimum kod, – hippiemancam

cevap

7

, orada yerleşik bir karşılaştırma operatörü. Sizinkini tanımladığınızda, yerleşik olanın otomatik olarak gizlenmesi gerekir.

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 ]

[over.built/1]

sorunuza gelince, evet, bu bir derleyici hata gibi görünüyor.

+0

Bunun için teşekkürler, sadece delirmediğimi bilmek güzel. – hippiemancam

+0

Yerleşik adayların çalışma şekli böyle değil. –

+0

@ T.C. - Yanlış paragraflar (şimdi sabit), ancak derleyici hata yine de alıntı. – StoryTeller

İlgili konular