私は私のC++コンパイラは次のコードと混同される理由を理解しようとしています:私はエラー:のためのあいまいなオーバーロード「演算子==」
$ c++ enum.cxx
enum.cxx: In function ‘int main()’:
enum.cxx:30:10: error: ambiguous overload for ‘operator==’ (operand types are ‘Enum’ and ‘Enum’)
b = t1 == t2;
^
enum.cxx:30:10: note: candidates are:
enum.cxx:30:10: note: operator==(Enum::Type, Enum::Type) <built-in>
enum.cxx:30:10: note: operator==(int, int) <built-in>
:
struct Enum
{
enum Type
{
T1,
T2
};
Enum(Type t):t_(t){}
operator Type() const { return t_; }
private:
Type t_;
// prevent automatic conversion for any other built-in types such as bool, int, etc
template<typename T> operator T() const;
};
enum Type2
{
T1,
T2
};
int main()
{
bool b;
Type2 e1 = T1;
Type2 e2 = T2;
b = e1 == e2;
Enum t1 = Enum::T1;
Enum t2 = Enum::T2;
b = t1 == t2;
return 0;
}
コンパイルにつながりますoperator==
:
bool operator==(Enum const &rhs) { return t_ == rhs.t_; }
しかし、私がこの症状を解決できることを理解してください私が探しているのは、なぜenum
を比較するのかという解釈は、class
の中で行われたときにのみあいまいさにつながります。私はこの小さなenumラッパーを書いています。私のコードではC++ 03のみを使用する必要があるからです。
'Enum'コンバートを**任意の**型にする演算子T()があります。それはあいまいさをたくさん招きます。 –