Topic: Proposal for amendment to C/C++ standard regarding conditional operator


Author: MichelColman@mac.com (Michel Colman)
Date: Wed, 29 Nov 2006 15:48:48 GMT
Raw View
This is my first post here (in fact my first newsgroup post ever) so
I hope I'm not doing anything wrong...

I have a proposal for an amendment to the C/C++ standards.

The conditional operator '?' should be changed so that it can upgrade
both the second and third operand to a common type if both differ
only by their cv-qualifications.

For example:

{
    int ** a;
    const int ** b;
    1 ? a : b;
}

Under the current standards, this is ill-formed since neither
variable can be converted to the type of the other. (Nope, int**
cannot be converted to const int**, as described with an example in
the standard)

However, both operands can easily be converted to the common type
`const int * const *'. I think the standard should automatically
perform this conversion instead of declaring the expression ill-
formed. With the current standards (as far as I know), conversion of
both operands to a common type is only attempted in very specific
situations with class- or enum-typed operands. This is confirmed by
error messages in GCC 4.

I would propose the following:

If the types of the second and third operand of the conditional
operator differ only by their cv-qualifications, both will be
converted to the common type with the smallest set of cv-
qualifications compatible with both types. For example,
`char***const***' and `char******' will both be converted to
`char***const*const*const*'

Obviously, the result would not be an lvalue since the operands have
different types.

To go even further, one might propose the following should be possible:
{
    class A {};
    class B: public A {};
    class C: public A {};
    bool condition;
    B* b;
    C* c;
    A* a = condition ? b : c;
}

The result of "condition ? b : c" should be type "A*". However, this
may be more difficult to implement, I haven't taken things like
multiple inheritance into account etc., so there may be technical
difficulties I haven't thought of.

The first proposal, however, should pose no difficulties whatsoever.
In fact, I am writing a simple compiler for a course project and
intuitively implemented it this way, only to find out it did not
agree with gcc and the C++ standard. I then had to re-write the code
to produce an error message :-(

Michel Colman


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]