The following program should not compile (without warnings) on two counts, but
in fact only fails on one, and not for the right reason, as far as I can see.

class C
{
public:
  C(const char *);
  operator const char *();
};

void
foo (bool b)
{
  // Prove that the implicit convertions work both ways.
  C c = "1";
  const char * s = C("2");

  // This is ambiguous; both operands may be converted to the other's type.
  // However, the compiler accepts it without a diagnostic.
  b ? "1" : C("2");

  // This is basically the same as the previous statement, but it fails with
  // the error "operands to ?: have different types".
  b ? c : s;
}

Both conditional operators should fall foul of clause 5.16, paragraph 3, of the
C++ standard. The last two operands are of different types, and each may be
implicitly converted to the opposite, as demonstrated by the assignments above.

The second conditional operator looks like a different way of writing the
first, but fails with an error stating that the operands have different types.
They do have different types, but again, the conversions should apply just the
same.

If I alter the program to use a type other than "const char *" (such as int, or
another class type) then I get different results again, which is surprising.


-- 
           Summary: Condition operator ?: and ambiguous convertions
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: andrew dot stubbs at st dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32019

Reply via email to