Avoid implicit cast from enum to double

Discussion in 'C++' started by Flo, Jul 18, 2006.

  1. Flo

    Flo Guest

    I have the problem, that the following Code produces neither warning
    nor error at compile time, at least not with the Microsoft Visual C++
    6.0 compiler (warning level 4)

    enum E { eOne, eTwo };
    struct S {
    void fooDouble( double ) {};
    void fooEnum( E ) {};
    };
    int main() {
    S s;
    s.fooDouble( eOne ); // neither warning nor error
    return 0;
    }

    As you can see, accidentally an enum is passed to the method expecting
    an double. In my real program, this leads to an error at run-time,
    since fooDouble only accepts quit small values, i.e. values smaller
    than 1.0. Has anybody an idea what I can do, maybe changes in the
    methodnames/parameterlist, to provoke a warning at compile time so one
    can not accidentally pass an enum to the method expecting a double?

    Flo

    Flo
    Flo, Jul 18, 2006
    #1
    1. Advertising

  2. Flo wrote:
    > I have the problem, that the following Code produces neither warning
    > nor error at compile time, at least not with the Microsoft Visual C++
    > 6.0 compiler (warning level 4)
    >
    > enum E { eOne, eTwo };
    > struct S {
    > void fooDouble( double ) {};
    > void fooEnum( E ) {};


    Please drop the habit of appending a semicolon after a function body.
    It just looks sloppy.

    > };
    > int main() {
    > S s;
    > s.fooDouble( eOne ); // neither warning nor error
    > return 0;
    > }
    >
    > As you can see, accidentally an enum is passed to the method expecting
    > an double. In my real program, this leads to an error at run-time,
    > since fooDouble only accepts quit small values, i.e. values smaller
    > than 1.0. Has anybody an idea what I can do, maybe changes in the
    > methodnames/parameterlist, to provoke a warning at compile time so one
    > can not accidentally pass an enum to the method expecting a double?


    Wrap your enum in a struct and pass the struct. Inside just access the
    wrapped value.

    Check the value (or assert if you can debug all cases of using your
    function) inside your 'fooDouble' function.

    Be careful and just don't write code that passes the enum to 'fooDouble'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 18, 2006
    #2
    1. Advertising

  3. Flo

    Rolf Magnus Guest

    Flo wrote:

    > I have the problem, that the following Code produces neither warning
    > nor error at compile time, at least not with the Microsoft Visual C++
    > 6.0 compiler (warning level 4)
    >
    > enum E { eOne, eTwo };
    > struct S {
    > void fooDouble( double ) {};
    > void fooEnum( E ) {};
    > };
    > int main() {
    > S s;
    > s.fooDouble( eOne ); // neither warning nor error
    > return 0;
    > }


    Well, it can be converted implicitly.

    > As you can see, accidentally an enum is passed to the method expecting
    > an double. In my real program, this leads to an error at run-time,
    > since fooDouble only accepts quit small values, i.e. values smaller
    > than 1.0. Has anybody an idea what I can do, maybe changes in the
    > methodnames/parameterlist, to provoke a warning at compile time so one
    > can not accidentally pass an enum to the method expecting a double?


    Remove the type prefix from the function name and call your functions both
    just foo. Then the compiler will automatically choose the right function.
    Rolf Magnus, Jul 18, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ferran
    Replies:
    9
    Views:
    3,027
    Kevin Goodsell
    Apr 12, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,483
    Victor Bazarov
    Feb 17, 2005
  3. Replies:
    7
    Views:
    427
  4. Calum Grant
    Replies:
    2
    Views:
    362
    Neelesh Bodas
    Dec 7, 2005
  5. Olaf
    Replies:
    9
    Views:
    1,021
    Victor Bazarov
    Jul 27, 2007
Loading...

Share This Page