Re: plz xplain behavior of const_cast

Discussion in 'C++' started by Alf P. Steinbach, Jan 5, 2012.

  1. On 05.01.2012 08:01, Heck wrote:
    > const int NUM_UNIVS = -1;


    Don't use all uppercase for constants. That's a Java convention. Java
    does not have a preprocessor. C++ does have a preprocessor. In C++, all
    uppercase is conventionally used for and reserved for macros names,
    primarily to reduce the probability of name collisions.

    >
    > {
    > // some block of code, and at the end of the block,
    > // I now have the value I need for NUM_UNIVS,
    > // so I do the following to assign it the new value
    >
    > int * tmp;
    > *(tmp = const_cast<int*> (& NUM_UNIVS)) = new_value;


    To cast away constness of an object, just cast to a non-const reference;
    there's no need for the pointer stuff.

    However, if you cast away constness of and then attempt to modify an
    /originally const/ object, then you have Undefined Behavior.

    That's because you have told the compiler that it's originally const and
    will never be changed, so the compiler is free to substitute the
    (presumed) known value anywhere the object is used.

    Short advice: don't cast.


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Jan 5, 2012
    #1
    1. Advertising

  2. Alf P. Steinbach

    gwowen Guest

    On Jan 5, 7:57 am, "Alf P. Steinbach" <alf.p.steinbach
    > wrote:
    > On 05.01.2012 08:01, Heck wrote:
    >
    > >   const int    NUM_UNIVS =  -1;

    >
    > Don't use all uppercase for constants. That's a Java convention.


    Not really. It's a logical extension of the C rules/conventions that
    say:
    (i) Macros are conventially capitals.
    (ii) In C, const variables of integer type cannot be used as compile
    time constants, and, prior to C99's VLAs, if you want non-dynamic
    array, you need a compile time constants.

    So integer constants tend to #define'd, instead of just defined.

    So compile-time constants tend to be capitals. Then that convention
    tends to leak across to C++ due to the similarity of the languages and
    the identical use cases.
    gwowen, Jan 5, 2012
    #2
    1. Advertising

  3. On 05.01.2012 13:00, gwowen wrote:
    > On Jan 5, 7:57 am, "Alf P. Steinbach"<alf.p.steinbach
    > > wrote:
    >> On 05.01.2012 08:01, Heck wrote:
    >>
    >>> const int NUM_UNIVS = -1;

    >>
    >> Don't use all uppercase for constants. That's a Java convention.

    >
    > Not really. It's a logical extension of the C rules/conventions that
    > say:
    > (i) Macros are conventially capitals.
    > (ii) In C, const variables of integer type cannot be used as compile
    > time constants, and, prior to C99's VLAs, if you want non-dynamic
    > array, you need a compile time constants.


    That's just bad programmers.

    C has had 'enum' since C89.

    'enum' works nicely for expressing whatever integer real constants.


    > So integer constants tend to #define'd, instead of just defined.


    It's true that the convention probably originated with C, although the
    early C practitioners used all kinds of weird conventions, including
    none. They were programming in-the-small. So it didn't matter so much.

    However, as you write, in C and C++ all uppercase is a convention for
    macros.

    Arguing that it is a convention for constants just because constants are
    sometimes expressed as macros, is just like arguing that it is for
    functions, or whatever that can be expressed as a macro.

    The convention does IMO not serve a useful purpose for constants, in
    particular in light of "one man's constant is another man's variable".

    The convention does serve a useful purpose for macros.


    > So compile-time constants tend to be capitals. Then that convention
    > tends to leak across to C++ due to the similarity of the languages and
    > the identical use cases.


    Hm, I don't think there are really many who start out with C and migrate
    up. Rather they migrate sideways/down from Java and C#. They don't pick
    up all these weird inheritance patterns and singletons and abstract
    factories and managers and whatnot in C, I think.

    Cheers,

    - Alf
    Alf P. Steinbach, Jan 5, 2012
    #3
  4. Alf P. Steinbach

    Ian Collins Guest

    On 01/ 6/12 11:17 AM, Alf P. Steinbach wrote:
    > On 05.01.2012 13:00, gwowen wrote:
    >
    >> So compile-time constants tend to be capitals. Then that convention
    >> tends to leak across to C++ due to the similarity of the languages and
    >> the identical use cases.

    >
    > Hm, I don't think there are really many who start out with C and migrate
    > up. Rather they migrate sideways/down from Java and C#. They don't pick
    > up all these weird inheritance patterns and singletons and abstract
    > factories and managers and whatnot in C, I think.


    I think you would be surprised Alf. Most of the aspirational C++
    programmers I work with or coach are embedded C programmers looking to
    make the next step.

    --
    Ian Collins
    Ian Collins, Jan 5, 2012
    #4
  5. On Jan 5, 10:17 pm, "Alf P. Steinbach" <alf.p.steinbach
    > wrote:
    > On 05.01.2012 13:00, gwowen wrote:
    > > On Jan 5, 7:57 am, "Alf P. Steinbach"<alf.p.steinbach
    > > >  wrote:
    > >> On 05.01.2012 08:01, Heck wrote:

    >
    > >>>    const int    NUM_UNIVS =  -1;

    >
    > >> Don't use all uppercase for constants. That's a Java convention.

    >
    > > Not really. It's a logical extension of the C rules/conventions that
    > > say:
    > > (i) Macros are conventially capitals.
    > > (ii) In C, const variables of integer type cannot be used as compile
    > > time constants, and, prior to C99's VLAs, if you want non-dynamic
    > > array, you need a compile time constants.

    >
    > That's just bad programmers.
    >
    > C has had 'enum' since C89.
    >
    > 'enum' works nicely for expressing whatever integer real constants.
    >
    > > So integer constants tend to #define'd, instead of just defined.

    >
    > It's true that the convention probably originated with C, although the
    > early C practitioners used all kinds of weird conventions, including
    > none. They were programming in-the-small. So it didn't matter so much.


    small. Like Unix...

    > However, as you write, in C and C++ all uppercase is a convention for
    > macros.
    >
    > Arguing that it is a convention for constants just because constants are
    > sometimes expressed as macros, is just like arguing that it is for
    > functions, or whatever that can be expressed as a macro.
    >
    > The convention does IMO not serve a useful purpose for constants, in
    > particular in light of "one man's constant is another man's variable".
    >
    > The convention does serve a useful purpose for macros.
    >
    > > So compile-time constants tend to be capitals. Then that convention
    > > tends to leak across to C++ due to the similarity of the languages and
    > > the identical use cases.

    >
    > Hm, I don't think there are really many who start out with C and migrate
    > up.


    seriously? I'd have thought there were loads. I'm one. Weren't all the
    initial C++ programmers originally C programmers?

    > Rather they migrate sideways/down from Java and C#. They don't pick
    > up all these weird inheritance patterns and singletons and abstract
    > factories and managers and whatnot in C, I think.


    don't singletons and abstract factory come from GoF?
    Nick Keighley, Jan 6, 2012
    #5
  6. gwowen <> wrote:
    > Not really. It's a logical extension of the C rules/conventions that
    > say:
    > (i) Macros are conventially capitals.
    > (ii) In C, const variables of integer type cannot be used as compile
    > time constants, and, prior to C99's VLAs, if you want non-dynamic
    > array, you need a compile time constants.
    >
    > So integer constants tend to #define'd, instead of just defined.
    >
    > So compile-time constants tend to be capitals. Then that convention
    > tends to leak across to C++ due to the similarity of the languages and
    > the identical use cases.


    There's a difference between a compile-time constant in C++ and a
    #define macro. For one, you can take the address of the former (ie.
    create a pointer that points to the const) but not the latter.
    Juha Nieminen, Jan 6, 2012
    #6
  7. Alf P. Steinbach

    gwowen Guest

    On Jan 6, 8:46 am, Nick Keighley <>
    wrote:

    > > Hm, I don't think there are really many who start out with C and migrate
    > > up.

    >
    > seriously? I'd have thought there were loads. I'm one.


    Me too

    > Weren't all the initial C++ programmers originally C programmers?


    Even if its true of the latest crop of programmers, its probably not
    true of their teachers, or their teachers's teachers, and the people
    who wrote their textbooks. Its easy to pick habits up from that.
    Besides, K&R2 uses #define where Alf says "only bad programmers" do
    that.
    gwowen, Jan 6, 2012
    #7
  8. Alf P. Steinbach

    Jorgen Grahn Guest

    On Thu, 2012-01-05, Alf P. Steinbach wrote:
    ....
    > Hm, I don't think there are really many who start out with C and migrate
    > up. Rather they migrate sideways/down from Java and C#. They don't pick
    > up all these weird inheritance patterns and singletons and abstract
    > factories and managers and whatnot in C, I think.


    I happen to dislike much of that stuff too, but I think it's unfair to
    describe it as a virus from the Java world. When people come to their
    first language which supports object-oriented programming, they will
    want to learn about that OOP stuff, and they will find those things
    there.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Jan 7, 2012
    #8
  9. Jorgen Grahnæ–¼ 2012å¹´1月7日星期六UTC+8下åˆ2時38分32秒寫é“:
    > On Thu, 2012-01-05, Alf P. Steinbach wrote:
    > ...
    > > Hm, I don't think there are really many who start out with C and migrate
    > > up. Rather they migrate sideways/down from Java and C#. They don't pick
    > > up all these weird inheritance patterns and singletons and abstract
    > > factories and managers and whatnot in C, I think.

    >
    > I happen to dislike much of that stuff too, but I think it's unfair to
    > describe it as a virus from the Java world. When people come to their
    > first language which supports object-oriented programming, they will
    > want to learn about that OOP stuff, and they will find those things
    > there.
    >
    > /Jorgen


    The object programming in C++ can be summarized as follows:

    1. class inheritance and object properties set up
    by the compiler for all static parts and a bunch of static functors with
    *this as the hidden parameter

    2. one vtable in a class and one vpointer in an object to be used to
    invoke the right function pointer as a virtual function at the run-time

    3. the operator reloading part similar to smalltalk only in syntax but not very helpful in developing cad tools or code generators





    > --
    > // Jorgen Grahn <grahn@ Oo o. . .
    > \X/ snipabacken.se> O o .
    88888 Dihedral, Jan 7, 2012
    #9
    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. brian

    Size of my Struct? PLZ PLZ reply

    brian, Nov 23, 2004, in forum: C Programming
    Replies:
    7
    Views:
    408
    -berlin.de
    Nov 25, 2004
  2. Replies:
    1
    Views:
    367
  3. Replies:
    2
    Views:
    325
    Nick Keighley
    Nov 24, 2006
  4. rincyrose

    can any 1 xplain this

    rincyrose, Oct 26, 2008, in forum: C Programming
    Replies:
    0
    Views:
    253
    rincyrose
    Oct 26, 2008
  5. Nephi Immortal

    Const_cast as undefined behavior?

    Nephi Immortal, Jan 9, 2013, in forum: C++
    Replies:
    35
    Views:
    634
    Gerhard Fiedler
    Jan 19, 2013
Loading...

Share This Page