@@ -1230,8 +1230,10 @@ convert_class_to_reference (tree reference_type, tree s, tree expr, int flags)
rvalue of the right type is good enough. */
tree f = cand->fn;
tree t2 = TREE_TYPE (TREE_TYPE (f));
- if (TREE_CODE (t2) != REFERENCE_TYPE
- || !reference_compatible_p (t, TREE_TYPE (t2)))
+ if (cand->viable == 0)
+ /* Don't bother looking more closely. */;
+ else if (TREE_CODE (t2) != REFERENCE_TYPE
+ || !reference_compatible_p (t, TREE_TYPE (t2)))
{
/* No need to set cand->reason here; this is most likely
an ambiguous match. If it's not, either this candidate
new file mode 100644
@@ -0,0 +1,14 @@
+// PR c++/46831
+// { dg-options -std=c++0x }
+
+struct B { };
+struct D : B { };
+struct A {
+ template<typename T = void> operator D&();
+ operator long();
+};
+
+void f(long);
+void f(B&);
+
+int main() { f(A()); }