static_cast

Discussion in 'C++' started by Rahul, Sep 14, 2008.

  1. Rahul

    Rahul Guest

    Hi,

    Everywhere I read that static_cast<> only work fine for the conversion
    which are implicitly allowed by the compiler
    hence the following does not work
    int *i;
    double *d;
    d = i; // Compilation error ,
    OK
    i= static_cast<int *>(d); // Compilation error. OK

    But surprisingly the following static_cast from Base * to Derived *
    works (assume class Derived: public Base)
    Base *bp=new Base();
    Derived *dp=new Derived();
    bp= dp; // Error, compiler does not allow this, This is OK
    dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
    compiler allow this (I tested in VC++ and gcc)
     
    Rahul, Sep 14, 2008
    #1
    1. Advertising

  2. Rahul

    Kai-Uwe Bux Guest

    Rahul wrote:

    > Hi,
    >
    > Everywhere I read that static_cast<> only work fine for the conversion
    > which are implicitly allowed by the compiler
    > hence the following does not work
    > int *i;
    > double *d;
    > d = i; // Compilation error ,
    > OK
    > i= static_cast<int *>(d); // Compilation error. OK


    Ahem, no: the mentioned reason is not recognized by the standard.

    The reason that the above does not work is that this conversion is not
    mentioned in [5.2.9].


    > But surprisingly the following static_cast from Base * to Derived *
    > works (assume class Derived: public Base)
    > Base *bp=new Base();
    > Derived *dp=new Derived();
    > bp= dp; // Error, compiler does not allow this, This is OK
    > dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
    > compiler allow this (I tested in VC++ and gcc)


    See [5.2.9/8].

    However, you can only use this safely (i.e., without undefined behavior) if
    you _know_ for other reasons that the pointer to base _is_ indeed a pointer
    to derived. If you cannot be sure, you better use dynamic_cast<> and test
    for 0.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 14, 2008
    #2
    1. Advertising

  3. Kai-Uwe Bux wrote:
    >> But surprisingly the following static_cast from Base * to Derived *
    >> works (assume class Derived: public Base)
    >> Base *bp=new Base();
    >> Derived *dp=new Derived();
    >> bp= dp; // Error, compiler does not allow this, This is OK
    >> dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
    >> compiler allow this (I tested in VC++ and gcc)

    >
    > See [5.2.9/8].


    Btw, another situation where implicit cast fails and static_cast is
    valid is when casting a void* to some other type of pointer.
     
    Juha Nieminen, Sep 14, 2008
    #3
  4. Rahul

    James Kanze Guest

    On Sep 14, 10:27 pm, Rahul <> wrote:
    > Everywhere I read that static_cast<> only work fine for the
    > conversion which are implicitly allowed by the compiler


    Where's everywhere. I've never read that.

    As a very rough first approximation, static_cast can be used
    where ever the converision or its inverse would be implicitly
    allowed, and const-ness is preserved.

    > hence the following does not work
    > int *i;
    > double *d;
    > d = i; // Compilation error ,
    > OK
    > i= static_cast<int *>(d); // Compilation error. OK


    Since the two pointers are to unrelated types, there is no
    conversion in either direction, and the static cast is
    forbidden. static_cast< void* >( d ) is legal, however (since
    there is an implicite conversion double* to void), as if
    static_cast< double* >( ptrToVoid ), since this is the inverse
    of an implicite conversion (and doesn't affect const-ness).

    > But surprisingly the following static_cast from Base * to
    > Derived * works (assume class Derived: public Base)
    > Base *bp=new Base();
    > Derived *dp=new Derived();
    > bp= dp; // Error, compiler does not allow this, This is OK
    > dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
    > compiler allow this (I tested in VC++ and gcc)


    Because there is an implicit conversion Derived* to Base*, and
    you're not removing const. (Also because this particular case
    is spelled out explicitly in the standard. In fact, all of the
    cases are more or less spelled out explicitly, since it isn't
    sufficient for the standard to say that the cast is legal; it
    also has to specify its semantics.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 15, 2008
    #4
    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. Chandra Shekhar Kumar

    static_cast confusion

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    7
    Views:
    2,042
    Mirek Fidler
    Jun 27, 2003
  2. Wenjie
    Replies:
    4
    Views:
    431
    Victor Bazarov
    Jul 12, 2003
  3. alg

    static_cast<>

    alg, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    504
  4. Bo Peng
    Replies:
    11
    Views:
    1,111
    Victor Bazarov
    Oct 20, 2006
  5. junyangzou
    Replies:
    13
    Views:
    276
Loading...

Share This Page