const_cast

Discussion in 'C++' started by asit, May 4, 2011.

  1. asit

    asit Guest

    is there any way to know if const_cast fails ??
    asit, May 4, 2011
    #1
    1. Advertising

  2. asit

    Noah Roberts Guest

    On 5/4/2011 11:40 AM, asit wrote:
    > is there any way to know if const_cast fails ??


    It won't compile. There's actually no such thing as failing to remove
    constness. No, there's no way to check if doing so will turn the world
    inside out.

    --
    http://crazycpp.wordpress.com
    Noah Roberts, May 4, 2011
    #2
    1. Advertising

  3. * asit:
    > is there any way to know if const_cast fails ??


    const_cast can't "fail", other than with a compiler error if you're
    casting away more than just 'const'. However, this doesn't mean that
    the resulting code will actually work. It may still trigger undefined
    behaviour, and there's no way to "detect" that at runtime.

    In that respect, const_cast *always* fails; you're throwing away a
    valuable contract, and is inherently dangerous. I suppose it is still
    preferable over a C-style cast, though.

    --
    Martijn van Buul -
    Martijn van Buul, May 4, 2011
    #3
  4. asit

    Qi Guest

    On 2011-5-5 2:40, asit wrote:
    > is there any way to know if const_cast fails ??


    Others had answered quite well.

    What I want to say is that const_cast is a compile time operator, not
    runtime operator.

    That means, if it "fails", the code won't get compiled.
    If the code is compiled, the operator won't fail any more at runtime.


    --
    WQ
    Qi, May 5, 2011
    #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. Kaspar Minosiants

    [help] const_cast

    Kaspar Minosiants, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    415
    John Harrison
    Jul 21, 2003
  2. drowned

    const_cast question

    drowned, Aug 4, 2003, in forum: C++
    Replies:
    3
    Views:
    473
    Josephine Schafer
    Aug 4, 2003
  3. R. Anbeeswaran

    const_cast<>

    R. Anbeeswaran, Nov 13, 2003, in forum: C++
    Replies:
    7
    Views:
    588
    Ekkehard Morgenstern
    Nov 14, 2003
  4. S.Senthilvel

    const_cast

    S.Senthilvel, Jan 6, 2004, in forum: C++
    Replies:
    4
    Views:
    2,025
    Andrey Tarasevich
    Jan 8, 2004
  5. Gajanan Bhat

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    537
    Bill Seurer
    May 5, 2004
Loading...

Share This Page