Topic: enum conversion to int


Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/08/11
Raw View
In article <3992F04C.B751F944@tribble.com>,
  David R Tribble <david@tribble.com> wrote:
> We recently ported some of our code to a new release of the compiler,
> which is closer to being ISO compliant that the previous release, and
> naturally we uncovered a few problems.  This one, though, needs some
> explaining.
>
[snip]
> The client source file contains code like this in several places:
>
>     extern State  getStatus();            // [F]
>
>     void proc()
>     {
>         if (getStatus() == State::OKAY)   // [T]
>         ...
>     }
>
[snip]
> My question is, why will the compiler use 'State::operator Val()'
> when comparing a State to a State::Val, but will not use
> 'State::operator int()'?  Is this consistent with the ISO C++
> type conversion rules?
I suspect a compiler bug.  operator== expects arithmetic types, and both
State and Val can be implicitly converted to an int with a single
conversion.

--
Jim
This message was posted using plain text only.  Any hyperlinks you may
see are not part of my message, and constitute a violation of copyright.
I do not endorse any products or services that may be linked to this
message.


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <david@tribble.com>
Date: 2000/08/11
Raw View
We recently ported some of our code to a new release of the compiler,
which is closer to being ISO compliant that the previous release, and
naturally we uncovered a few problems.  This one, though, needs some
explaining.

The header file for the class:

    class State
    {
    public:
        enum Val                  // [E]
        {
            OKAY, FAIL, ...
        };

        operator int() const      // [I]
        {
            return (int)m_val;
        };

    private:
        Val     m_val;
        ...
    };

The client source file contains code like this in several places:

    extern State  getStatus();            // [F]

    void proc()
    {
        if (getStatus() == State::OKAY)   // [T]
        ...
    }

The problem is that the conditional expression at [T] failed to
compile; the compiler stated that there was a type mismatch
(comparing a 'State' to a 'State::Val').

This comparison used to work, since the old compiler would convert
the result of getStatus(), of type State, into an int by calling
'State::operator int()', and also converted State::OKAY into an int.
The new compiler doesn't do this.

Our solution was to add the following operator to class State,
which provides an explicit conversion from State to State::Val
(which actually is more correct than the old code):

        // Added to class State...
        operator Val() const        // [V]
        {
            return m_val;
        }

My question is, why will the compiler use 'State::operator Val()'
when comparing a State to a State::Val, but will not use
'State::operator int()'?  Is this consistent with the ISO C++
type conversion rules?

--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]