reinterpret_cast and enumerations

Discussion in 'C++' started by Christopher Benson-Manica, Nov 2, 2004.

  1. enum foo {a,b,c,d};

    int main()
    {
    unsigned int bar=reinterpret_cast< unsigned int >( a );
    return bar;
    }

    g++ rejects the code based on the reinterpret_cast, so I presume it is
    not legal. Why not? And why should g++ accept the code when
    static_cast is used instead (as it does)?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 2, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica wrote:
    > enum foo {a,b,c,d};
    >
    > int main()
    > {
    > unsigned int bar=reinterpret_cast< unsigned int >( a );
    > return bar;
    > }
    >
    > g++ rejects the code based on the reinterpret_cast, so I presume it is
    > not legal. Why not?


    You will have to ask in comp.std.c++. The Standard lists conversions that
    are allowed to be performed by reinterpret_cast and all others are not
    allowed. Why that is so, is not explained in the Standard, and here, in
    comp.lang.c++ the answer to your 'why' question is "because the Standard
    says so".

    > And why should g++ accept the code when
    > static_cast is used instead (as it does)?


    Because static_cast has different set of restrictions and they are not the
    same as reinterpret_cast.

    V
    Victor Bazarov, Nov 2, 2004
    #2
    1. Advertising

  3. Christopher Benson-Manica

    Tom Widmer Guest

    On Tue, 2 Nov 2004 17:19:33 +0000 (UTC), Christopher Benson-Manica
    <> wrote:

    >enum foo {a,b,c,d};
    >
    >int main()
    >{
    > unsigned int bar=reinterpret_cast< unsigned int >( a );
    > return bar;
    >}
    >
    >g++ rejects the code based on the reinterpret_cast, so I presume it is
    >not legal. Why not? And why should g++ accept the code when
    >static_cast is used instead (as it does)?


    reinterpret_cast can't perform conversions between integral types,
    that's what static_cast is for. The point of the new C++ style casts
    is to provide different casts for different jobs.

    Tom
    Tom Widmer, Nov 2, 2004
    #3
  4. On Tue, 2 Nov 2004 17:19:33 +0000 (UTC), Christopher Benson-Manica
    <> wrote:

    >enum foo {a,b,c,d};
    >
    >int main()
    >{
    > unsigned int bar=reinterpret_cast< unsigned int >( a );
    > return bar;
    >}
    >
    >g++ rejects the code based on the reinterpret_cast, so I presume it is
    >not legal. Why not? And why should g++ accept the code when
    >static_cast is used instead (as it does)?


    It is totally unnecessary to cast here since the conversion is done
    automatically. Only to avoid comparing signed and unsigned values
    should there be a need to cast.

    In general, static_cast is used to cast between value types;
    reinterpret_cast is usually used for casting unrelated types. Others
    have pointed out that the standard defines different rules for the two
    kinds of cast.

    --
    Bob Hairgrove
    Bob Hairgrove, Nov 2, 2004
    #4
  5. Christopher Benson-Manica

    Siemel Naran Guest

    "Bob Hairgrove" <> wrote in message

    > In general, static_cast is used to cast between value types;
    > reinterpret_cast is usually used for casting unrelated types. Others
    > have pointed out that the standard defines different rules for the two
    > kinds of cast.


    Is it true to say that reinterpret_cast is used to cast between unrelated
    pointer/reference types. Whereas static_cast is used to cast between
    integer types, between float types, between parent/child pointers.
    Siemel Naran, Nov 3, 2004
    #5
  6. Christopher Benson-Manica

    Ron Natalie Guest

    Bob Hairgrove wrote:

    > In general, static_cast is used to cast between value types;
    > reinterpret_cast is usually used for casting unrelated types. Others
    > have pointed out that the standard defines different rules for the two
    > kinds of cast.


    My way of describing static_cast is that it supports two functions:
    1. Forcing a conversion that could happen anyway:
    double d = 4.5;
    cout << d - static_cast<int>(d);
    the double->int conversion is defined, so I'm just making it happen
    explciitly.

    2. Inverting a conversion that I am certain is valid.
    struct Base { };
    struct Derived : Base { };
    Derived d;
    Base* bp = &d; // defined implicit conversion.
    Derived* dp = static_cast<Derived*>(dp); // well-behaved inverse of defined conversion
    Ron Natalie, Nov 3, 2004
    #6
    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. Kobe
    Replies:
    3
    Views:
    542
    Tomás
    Feb 15, 2006
  2. Alex Vinokur
    Replies:
    4
    Views:
    538
    Jakob Bieling
    Mar 27, 2006
  3. Taran
    Replies:
    2
    Views:
    318
    Frederick Gotham
    Jul 25, 2006
  4. ciccio
    Replies:
    4
    Views:
    1,087
    James Kanze
    Apr 15, 2008
  5. Alex Vinokur
    Replies:
    1
    Views:
    561
Loading...

Share This Page