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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    547
    John Harrison
    Jul 21, 2003
  2. drowned

    const_cast question

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

    const_cast<>

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

    const_cast

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

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    773
    Bill Seurer
    May 5, 2004
  6. Tobias Kilian
    Replies:
    2
    Views:
    1,771
    Michiel Salters
    May 25, 2004
  7. johny smith

    const_cast, reinterpret_cast

    johny smith, Jun 24, 2004, in forum: C++
    Replies:
    18
    Views:
    3,724
    Old Wolf
    Jun 24, 2004
  8. AlesD
    Replies:
    6
    Views:
    1,714
    Rolf Magnus
    Jul 12, 2004
Loading...