Quick cast style question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    Quick cast style question

    If you had an unsigned int that needed to be cast to a const myClass*,
    would you use

    const myClass* a=reinterpret_c ast<const myClass*>(my_va l);

    or

    const myClass* a=(const myClass*)myVal;

    ?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Leor Zolman

    #2
    Re: Quick cast style question

    On Fri, 30 Apr 2004 21:05:43 +0000 (UTC), Christopher Benson-Manica
    <ataru@nospam.c yberspace.org> wrote:
    [color=blue]
    >If you had an unsigned int that needed to be cast to a const myClass*,
    >would you use
    >
    >const myClass* a=reinterpret_c ast<const myClass*>(my_va l);
    >
    >or
    >
    >const myClass* a=(const myClass*)myVal;[/color]

    Personally, I'd use the reinterpret_cas t, since the compiler will diagnose
    attempts to use it in a situation where one of the other new-style casts
    would be the right one. So the former is saying "do the cast if in fact we
    are truly requiring reinterpret, and the others wouldn't do, else tell me
    I've screwed up", while the latter says "just shut up and do it."
    -leor[color=blue]
    >
    >?[/color]

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Karthik

      #3
      Re: Quick cast style question

      Christopher Benson-Manica wrote:
      [color=blue]
      > If you had an unsigned int that needed to be cast to a const myClass*,
      > would you use
      >
      > const myClass* a=reinterpret_c ast<const myClass*>(my_va l);
      >
      > or
      >
      > const myClass* a=(const myClass*)myVal;
      >
      > ?
      >[/color]
      Somehow I am always sceptic with the usage of reinterpret_cas t here.
      I would go for static_cast or dynamic_cast , as necessary. Using
      reinterpret_cas t is dangerous and would probably tie you to a specific
      implementation.

      --
      Karthik
      Humans please 'removeme_' for my real email.

      Comment

      • bartek

        #4
        Re: Quick cast style question

        Christopher Benson-Manica <ataru@nospam.c yberspace.org> wrote in
        news:c6uf37$7gc $1@chessie.cirr .com:
        [color=blue]
        > If you had an unsigned int that needed to be cast to a const myClass*,
        > would you use
        >
        > const myClass* a=reinterpret_c ast<const myClass*>(my_va l);
        >
        > or
        >
        > const myClass* a=(const myClass*)myVal;[/color]

        C-style casts were provided for compatibility with old code. In C++ you
        should only use the 'new'-style casts, as they show your intents clear in
        the code. 'reinterpret_ca st' is there to show that something drastic is
        going on.

        Code editors should highlight it in bloody red... or even flashing bloody
        red.

        Cheers.
        --
        :: bartekd [at] o2 [dot] pl

        Comment

        • bartek

          #5
          Re: Quick cast style question

          Karthik <removeme_kayka ydreamz@yahoo.c om> wrote in news:4092c3bc$1
          @darkstar:
          [color=blue]
          > Christopher Benson-Manica wrote:
          >[color=green]
          >> If you had an unsigned int that needed to be cast to a const myClass*,
          >> would you use
          >>
          >> const myClass* a=reinterpret_c ast<const myClass*>(my_va l);
          >>
          >> or
          >>
          >> const myClass* a=(const myClass*)myVal;
          >>
          >> ?
          >>[/color]
          > Somehow I am always sceptic with the usage of reinterpret_cas t here.
          > I would go for static_cast or dynamic_cast , as necessary. Using
          > reinterpret_cas t is dangerous and would probably tie you to a specific
          > implementation.[/color]

          'reinterpret_ca st' is the only option in that situation. The compiler
          would refuse to compile the above code when 'static_cast' was used.
          'dynamic_cast', on the other hand, only works for casting
          pointers/references within an inheritance hierarchy.

          Cheers.
          --
          :: bartekd [at] o2 [dot] pl

          Comment

          • Leor Zolman

            #6
            Re: Quick cast style question

            On Fri, 30 Apr 2004 14:20:39 -0700, Karthik
            <removeme_kayka ydreamz@yahoo.c om> wrote:
            [color=blue]
            >Christopher Benson-Manica wrote:
            >[color=green]
            >> If you had an unsigned int that needed to be cast to a const myClass*,
            >> would you use
            >>
            >> const myClass* a=reinterpret_c ast<const myClass*>(my_va l);
            >>
            >> or
            >>
            >> const myClass* a=(const myClass*)myVal;
            >>
            >> ?
            >>[/color]
            >Somehow I am always sceptic with the usage of reinterpret_cas t here.
            >I would go for static_cast or dynamic_cast , as necessary. Using
            >reinterpret_ca st is dangerous and would probably tie you to a specific
            >implementation .[/color]

            You may want to try the different forms and examine the results. In fact,
            reinterpret_cas t was the correct choice here (of the different casts. I
            can't speak to the appropriateness here of using a cast, versus not using
            one, in the first place...)
            -leor


            --
            Leor Zolman --- BD Software --- www.bdsoft.com
            On-Site Training in C/C++, Java, Perl and Unix
            C++ users: download BD Software's free STL Error Message Decryptor at:
            An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

            Comment

            • jeffc

              #7
              Re: Quick cast style question


              "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
              news:c6uf37$7gc $1@chessie.cirr .com...[color=blue]
              > If you had an unsigned int that needed to be cast to a const myClass*,
              > would you use
              >
              > const myClass* a=reinterpret_c ast<const myClass*>(my_va l);
              >
              > or
              >
              > const myClass* a=(const myClass*)myVal;[/color]

              Prefer the former - it was added to C++ as an improvement over the latter.


              Comment

              • Christopher Benson-Manica

                #8
                Re: Quick cast style question

                Leor Zolman <leor@bdsoft.co m> spoke thus:
                [color=blue]
                > You may want to try the different forms and examine the results. In fact,
                > reinterpret_cas t was the correct choice here (of the different casts. I
                > can't speak to the appropriateness here of using a cast, versus not using
                > one, in the first place...)[/color]

                Well, the design decision that makes a cast necessary in the first
                place is probably of debatable merit. Given that, the cast is pretty
                much required...

                --
                Christopher Benson-Manica | I *should* know what I'm talking about - if I
                ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                Comment

                • Jake Montgomery

                  #9
                  Re: Quick cast style question



                  bartek wrote:[color=blue]
                  > Christopher Benson-Manica <ataru@nospam.c yberspace.org> wrote in
                  > news:c6uf37$7gc $1@chessie.cirr .com:
                  >
                  >[color=green]
                  >>If you had an unsigned int that needed to be cast to a const myClass*,
                  >>would you use
                  >>
                  >>const myClass* a=reinterpret_c ast<const myClass*>(my_va l);
                  >>
                  >>or
                  >>
                  >>const myClass* a=(const myClass*)myVal;[/color]
                  >
                  >
                  > C-style casts were provided for compatibility with old code. In C++ you
                  > should only use the 'new'-style casts, as they show your intents clear in
                  > the code. 'reinterpret_ca st' is there to show that something drastic is
                  > going on.
                  >
                  > Code editors should highlight it in bloody red... or even flashing bloody
                  > red.
                  >[/color]

                  I agree when casting classes, but I still like the old style casts when
                  doing numeric 'type change' cats:

                  int n1=5;
                  int n2=3;
                  double d1 = ((double)n1)/n2;

                  Where you suggesting a static_cast<> be used here? Personally, I find
                  that to be a bit much.


                  Comment

                  • Leor Zolman

                    #10
                    Re: Quick cast style question

                    On Sat, 01 May 2004 21:07:13 GMT, Jake Montgomery
                    <jacobmdrop_nos pamers@yahoo.co m> wrote:
                    [color=blue]
                    >I agree when casting classes, but I still like the old style casts when
                    >doing numeric 'type change' cats:
                    >
                    > int n1=5;
                    > int n2=3;
                    > double d1 = ((double)n1)/n2;
                    >
                    >Where you suggesting a static_cast<> be used here? Personally, I find
                    >that to be a bit much.[/color]

                    Well, if you don't like the new_style cast for aesthetic reasons, or if you
                    specifically don't want to be warned when the cast isn't doing what you
                    think it is supposed to do (a benefit of using new-style casts), then
                    consider this: You have a large app and you wish to locate all the places
                    you've performed a cast. Tell us how to do it, for certain, with your
                    old-style casting used all over the place, vs. new-style casts.

                    Note: assume the coders have been very sloppy with style, writing (type), (
                    type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.

                    Note #2: Hope you have a tool supporting good regular expression syntax if
                    you want to be exhaustive with this ;-)
                    -leor
                    [color=blue]
                    >[/color]

                    --
                    Leor Zolman --- BD Software --- www.bdsoft.com
                    On-Site Training in C/C++, Java, Perl and Unix
                    C++ users: download BD Software's free STL Error Message Decryptor at:
                    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                    Comment

                    • bartek

                      #11
                      Re: Quick cast style question

                      Jake Montgomery <jacobmdrop_nos pamers@yahoo.co m> wrote in
                      news:4094117A.9 090603@yahoo.co m:

                      (...)
                      [color=blue]
                      > I agree when casting classes, but I still like the old style casts
                      > when doing numeric 'type change' cats:
                      >
                      > int n1=5;
                      > int n2=3;
                      > double d1 = ((double)n1)/n2;
                      >
                      > Where you suggesting a static_cast<> be used here? Personally, I find
                      > that to be a bit much.[/color]

                      Actually in similar cases, where automatic conversion is possible, I'd
                      suggest using the 'fake' constructor syntax:

                      double d1 = double(n1) / double(n2);

                      --
                      :: bartekd [at] o2 [dot] pl

                      Comment

                      • Jake Montgomery

                        #12
                        Re: Quick cast style question


                        Leor Zolman wrote:[color=blue]
                        > On Sat, 01 May 2004 21:07:13 GMT, Jake Montgomery
                        > <jacobmdrop_nos pamers@yahoo.co m> wrote:
                        >
                        >[color=green]
                        >>I agree when casting classes, but I still like the old style casts when
                        >>doing numeric 'type change' cats:
                        >>
                        >> int n1=5;
                        >> int n2=3;
                        >> double d1 = ((double)n1)/n2;
                        >>
                        >>Where you suggesting a static_cast<> be used here? Personally, I find
                        >>that to be a bit much.[/color]
                        >
                        >
                        > Well, if you don't like the new_style cast for aesthetic reasons, or if you
                        > specifically don't want to be warned when the cast isn't doing what you
                        > think it is supposed to do (a benefit of using new-style casts), then
                        > consider this: You have a large app and you wish to locate all the places
                        > you've performed a cast. Tell us how to do it, for certain, with your
                        > old-style casting used all over the place, vs. new-style casts.
                        >
                        > Note: assume the coders have been very sloppy with style, writing (type), (
                        > type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.
                        >[/color]

                        For simple type conversions (which is what my post spoke of) your
                        task seems extremely contrived. I can never imagine being concerned
                        with every occurrence of a int to double cast in the entire app. Could
                        you come up with a real world situation?
                        [color=blue]
                        > Note #2: Hope you have a tool supporting good regular expression syntax if
                        > you want to be exhaustive with this ;-)
                        > -leor
                        >[/color]
                        Yes I do - I use the awesome, but soon-to-be-defunct-due-to-buyout
                        CodeWright. In the extremely unlikely case that I needed to, I could
                        indeed do a multiple file grep that would find every occurrence of (say)
                        a cast to double.


                        Comment

                        • Jake Montgomery

                          #13
                          Re: Quick cast style question

                          bartek wrote:[color=blue]
                          > Jake Montgomery <jacobmdrop_nos pamers@yahoo.co m> wrote in
                          > news:4094117A.9 090603@yahoo.co m:
                          >
                          > (...)
                          >
                          >[color=green]
                          >>I agree when casting classes, but I still like the old style casts
                          >>when doing numeric 'type change' cats:
                          >>
                          >> int n1=5;
                          >> int n2=3;
                          >> double d1 = ((double)n1)/n2;
                          >>
                          >>Where you suggesting a static_cast<> be used here? Personally, I find
                          >>that to be a bit much.[/color]
                          >
                          >
                          > Actually in similar cases, where automatic conversion is possible, I'd
                          > suggest using the 'fake' constructor syntax:
                          >
                          > double d1 = double(n1) / double(n2);
                          >[/color]

                          I have no problem with this syntax ... just habit, and lack of a good
                          reason to change, keep me using the (double)n1 syntax. I will admit,
                          that the 'fake' constructor syntax does make things a bit clearer. Are
                          there any differences between the two in terms of generated code?




                          Comment

                          • Leor Zolman

                            #14
                            Re: Quick cast style question

                            On Tue, 04 May 2004 00:09:29 GMT, Jake Montgomery
                            <jacobmdrop_nos pamers@yahoo.co m> wrote:
                            [color=blue][color=green]
                            >>
                            >> Well, if you don't like the new_style cast for aesthetic reasons, or if you
                            >> specifically don't want to be warned when the cast isn't doing what you
                            >> think it is supposed to do (a benefit of using new-style casts), then
                            >> consider this: You have a large app and you wish to locate all the places
                            >> you've performed a cast. Tell us how to do it, for certain, with your
                            >> old-style casting used all over the place, vs. new-style casts.
                            >>
                            >> Note: assume the coders have been very sloppy with style, writing (type), (
                            >> type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.
                            >>[/color]
                            >
                            > For simple type conversions (which is what my post spoke of) your
                            >task seems extremely contrived. I can never imagine being concerned
                            >with every occurrence of a int to double cast in the entire app. Could
                            >you come up with a real world situation?
                            >[/color]

                            In the general case, given potentially large-scale projects (code reviews,
                            etc), the question of how often casts are used /is/ a real-world situation.
                            If in your project, you know that the only way you're using casts is the
                            one way you're using them, then you might not personally have this issue.

                            (And the "task" in question was rather rhetorical, just to point out the
                            value of the unique keywords used for new-style casts.)
                            [color=blue][color=green]
                            >> Note #2: Hope you have a tool supporting good regular expression syntax if
                            >> you want to be exhaustive with this ;-)
                            >> -leor
                            >>[/color]
                            > Yes I do - I use the awesome, but soon-to-be-defunct-due-to-buyout
                            >CodeWright. In the extremely unlikely case that I needed to, I could
                            >indeed do a multiple file grep that would find every occurrence of (say)
                            >a cast to double.[/color]

                            Yes, but again, not everyone who might at some point find themselves
                            needing to locate all their cast operations would necessarily have the
                            right tool for the job, as well as the requisite skill with r.e.'s, at
                            their fingertips.
                            -leor

                            --
                            Leor Zolman --- BD Software --- www.bdsoft.com
                            On-Site Training in C/C++, Java, Perl and Unix
                            C++ users: download BD Software's free STL Error Message Decryptor at:
                            An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                            Comment

                            • bartek

                              #15
                              Re: Quick cast style question

                              Jake Montgomery <jacobmdrop_nos pamers@yahoo.co m> wrote in
                              news:4096DFD4.8 000701@yahoo.co m:
                              [color=blue]
                              > bartek wrote:[color=green]
                              >> Jake Montgomery <jacobmdrop_nos pamers@yahoo.co m> wrote in
                              >> news:4094117A.9 090603@yahoo.co m:
                              >>
                              >> (...)
                              >>
                              >>[color=darkred]
                              >>>I agree when casting classes, but I still like the old style casts
                              >>>when doing numeric 'type change' cats:
                              >>>
                              >>> int n1=5;
                              >>> int n2=3;
                              >>> double d1 = ((double)n1)/n2;
                              >>>
                              >>>Where you suggesting a static_cast<> be used here? Personally, I find
                              >>>that to be a bit much.[/color]
                              >>
                              >>
                              >> Actually in similar cases, where automatic conversion is possible, I'd
                              >> suggest using the 'fake' constructor syntax:
                              >>
                              >> double d1 = double(n1) / double(n2);
                              >>[/color]
                              >
                              > I have no problem with this syntax ... just habit, and lack of a good
                              > reason to change, keep me using the (double)n1 syntax. I will admit,
                              > that the 'fake' constructor syntax does make things a bit clearer. Are
                              > there any differences between the two in terms of generated code?[/color]

                              The compiler I'm using generates exactly the same code in either case.
                              --
                              :: bartekd [at] o2 [dot] pl

                              Comment

                              Working...