Cast to void???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Theese

    Cast to void???

    In Loki (smartptr.h), I've noticed the following expression occurring by
    itself:

    (void)val;

    What is the purpose in casting something to void, especially in a
    stand-alone expression such as this and not as part of something larger?
    I'm surprised this is possible as it is not possible to declare a variable
    of type void. And I'm intrigued to learn the purpose...

    Here's the context I see this in:

    static void OnDereference(P val)
    { assert(val); (void)val; }

    BTW, I see this in the VC++ port of Loki. I don't know if it's in the
    original Loki or not...

    Thanks,
    Dave


  • WW

    #2
    Re: Cast to void???

    Dave Theese wrote:[color=blue]
    > In Loki (smartptr.h), I've noticed the following expression occurring
    > by itself:
    >
    > (void)val;
    >
    > What is the purpose in casting something to void, especially in a
    > stand-alone expression such as this and not as part of something
    > larger? I'm surprised this is possible as it is not possible to
    > declare a variable of type void. And I'm intrigued to learn the
    > purpose...[/color]
    [SNIP]

    First time I see this and the only reason I can imagine is to avoid compile
    time warnings for unused variables.

    --
    WW aka Attila


    Comment

    • John Almer

      #3
      Re: Cast to void???

      > Dave Theese wrote:[color=blue][color=green]
      > > In Loki (smartptr.h), I've noticed the following expression occurring
      > > by itself:
      > >
      > > (void)val;
      > >
      > > What is the purpose in casting something to void, especially in a
      > > stand-alone expression such as this and not as part of something
      > > larger? I'm surprised this is possible as it is not possible to
      > > declare a variable of type void. And I'm intrigued to learn the
      > > purpose...[/color]
      > [SNIP]
      >
      > First time I see this and the only reason I can imagine is to avoid[/color]
      compile[color=blue]
      > time warnings for unused variables.[/color]

      No need for this however. An unused parameter name can simply be omitted.


      Comment

      • WW

        #4
        Re: Cast to void???

        John Almer wrote:
        [SNIP][color=blue][color=green]
        >>
        >> First time I see this and the only reason I can imagine is to avoid
        >> compile time warnings for unused variables.[/color]
        >
        > No need for this however. An unused parameter name can simply be
        > omitted.[/color]

        Yes. But did I talk about a paramneter?

        It is deep inside template metaprogramming magic. I can easily imagine it
        was some sort of variable not used in a function in certain cases.

        --
        WW aka Attila


        Comment

        • John Almer

          #5
          Re: Cast to void???

          > > No need for this however. An unused parameter name can simply be[color=blue][color=green]
          > > omitted.[/color]
          >
          > Yes. But did I talk about a paramneter?[/color]

          No, but the op did.
          [color=blue]
          > It is deep inside template metaprogramming magic. I can easily imagine it
          > was some sort of variable not used in a function in certain cases.[/color]

          Probably. However, templates aren't "magic". They generate ordinary code no
          different than hand-written code. And no self-respecting programmer would
          ever use such a technique. However, there are no absolutes in programming
          and things aren't always so cut and dry (especially where metaprogramming is
          concerned). So I would never rule out the need for this type of construct
          under some very rare circumstance. It's obfuscating however and more likely
          than not a sign of bad design (in most cases anyway).


          Comment

          • WW

            #6
            Re: Cast to void???

            John Almer wrote:[color=blue][color=green][color=darkred]
            >>> No need for this however. An unused parameter name can simply be
            >>> omitted.[/color]
            >>
            >> Yes. But did I talk about a paramneter?[/color]
            >
            > No, but the op did.
            >[color=green]
            >> It is deep inside template metaprogramming magic. I can easily
            >> imagine it was some sort of variable not used in a function in
            >> certain cases.[/color]
            >
            > Probably. However, templates aren't "magic".[/color]

            No, they aren't. What I was talking about is template metaprogramming
            magic. A term of 3 words.
            [color=blue]
            > They generate ordinary code no different than hand-written code.[/color]

            Really? How about two phase name lookup? Dependent names? Not being
            compiled until instantiated? Compile time programming using template
            metaprogramming being a Turing capable language?
            [color=blue]
            > And no self-respecting
            > programmer would ever use such a technique.[/color]

            What technique????
            [color=blue]
            > However, there are no
            > absolutes in programming and things aren't always so cut and dry
            > (especially where metaprogramming is concerned). So I would never
            > rule out the need for this type of construct under some very rare
            > circumstance. It's obfuscating however and more likely than not a
            > sign of bad design (in most cases anyway).[/color]

            You should not rule it out. Since the code (whiuch I have appartenly missed
            to read carefully at first) looks like this:

            static void OnDereference(P val)
            { assert(val); (void)val; }


            How will you program the assert without a name for the argument???

            How will you avoid the warning when the argument is not used in production
            builds since the assert is preprocessed to nothing?

            --
            WW aka Attila


            Comment

            • Julián Albo

              #7
              Re: Cast to void???

              John Almer escribió:
              [color=blue][color=green]
              > > First time I see this and the only reason I can imagine is to avoid
              > > compile time warnings for unused variables.[/color]
              > No need for this however. An unused parameter name can simply be omitted.[/color]

              The parameter is used by the assert, then can't be omitted. The warning
              about para meter unused will come in the non debug compilation.

              Regrads.

              Comment

              • John Almer

                #8
                Re: Cast to void???

                > > They generate ordinary code no different than hand-written code.[color=blue]
                >
                > Really?[/color]

                Yes, really. You're confusing the mechanics of templates with the code it
                produces. A template is just a boilerplate for ordinary code. It doesn't
                force you to write unnecessary or extraneous constructs.
                [color=blue]
                >[color=green]
                > > And no self-respecting
                > > programmer would ever use such a technique.[/color]
                >
                > What technique????[/color]

                Casting to a void to eliminate a compiler warning.
                [color=blue]
                >[color=green]
                > > However, there are no
                > > absolutes in programming and things aren't always so cut and dry
                > > (especially where metaprogramming is concerned). So I would never
                > > rule out the need for this type of construct under some very rare
                > > circumstance. It's obfuscating however and more likely than not a
                > > sign of bad design (in most cases anyway).[/color]
                >
                > You should not rule it out. Since the code (whiuch I have appartenly[/color]
                missed[color=blue]
                > to read carefully at first) looks like this:
                >
                > static void OnDereference(P val)
                > { assert(val); (void)val; }
                >
                >
                > How will you program the assert without a name for the argument???
                > How will you avoid the warning when the argument is not used in production
                > builds since the assert is preprocessed to nothing?[/color]

                #ifdef NDEBUG
                static void OnDereference(P )
                {
                #else
                static void OnDereference(P val)
                {
                assert(val);
                #endif

                // ...
                }

                While certainly more verbose (and there are some variations on this), it
                makes it a lot clearer that the arg isn't used in production. Had it been
                done this way in the first place then the op would have either recognized it
                or asked about unused (nameless) parameters instead.


                Comment

                • WW

                  #9
                  Re: Cast to void???

                  John Almer wrote:[color=blue][color=green][color=darkred]
                  >>> They generate ordinary code no different than hand-written code.[/color]
                  >>
                  >> Really?[/color]
                  >
                  > Yes, really. You're confusing the mechanics of templates with the
                  > code it produces.[/color]

                  I have never talked about code generated by templates.
                  [color=blue]
                  > A template is just a boilerplate for ordinary code.[/color]

                  Ordinary code. You mean ordinary code (template metaprogramming we talk
                  about) which might never appear in the final executable and is really
                  executed runtime?
                  [color=blue]
                  > It doesn't force you to write unnecessary or extraneous constructs.[/color]

                  Where did I say that?
                  [color=blue][color=green][color=darkred]
                  >>> And no self-respecting
                  >>> programmer would ever use such a technique.[/color]
                  >>
                  >> What technique????[/color]
                  >
                  > Casting to a void to eliminate a compiler warning.[/color]

                  There was no other way. BTW I would love to see what would Andrei's
                  reaction to be on that. He is self-respecting. :-)
                  [color=blue][color=green]
                  >> static void OnDereference(P val)
                  >> { assert(val); (void)val; }
                  >>
                  >>
                  >> How will you program the assert without a name for the argument???
                  >> How will you avoid the warning when the argument is not used in
                  >> production builds since the assert is preprocessed to nothing?[/color]
                  >
                  > #ifdef NDEBUG
                  > static void OnDereference(P )
                  > {
                  > #else
                  > static void OnDereference(P val)
                  > {
                  > assert(val);
                  > #endif
                  >
                  > // ...
                  > }[/color]

                  Holy shit! Turn your code into an ifdef madness just because your knee-jerk
                  reaction does not like casting to void?
                  [color=blue]
                  > While certainly more verbose[/color]

                  And breaking the once and only once rule...

                  And a call for maintenance nightmare...

                  And ugly...
                  [color=blue]
                  > (and there are some variations on this),[/color]

                  Please do not show them.
                  [color=blue]
                  > it makes it a lot clearer that the arg isn't used in production.[/color]

                  Not for me. For me it makes it messy and unreadable and highly error prone
                  due to duplication.
                  [color=blue]
                  > Had
                  > it been done this way in the first place then the op would have
                  > either recognized it or asked about unused (nameless) parameters
                  > instead.[/color]

                  Had it been written this way ogirinally I would have mailed Andrei asking
                  hom why does he write ugly code and duplicated when he does not need to.
                  And it wasn't only me mailing him.

                  Loki internals are not for the faint hearthed or the beginner. For me it
                  took less than a second to figure out what it means - and I am not a guru.
                  And let me change your sentence: did the OP look it up what assert is he
                  could have deduced the whole idea himself. As I did. It helps if you
                  think.

                  --
                  WW aka Attila


                  Comment

                  • Kevin Goodsell

                    #10
                    Re: Cast to void???

                    Dave Theese wrote:
                    [color=blue]
                    > In Loki (smartptr.h), I've noticed the following expression occurring by
                    > itself:
                    >
                    > (void)val;
                    >[/color]

                    This is sometimes used to silence warnings from static code analysis
                    tools (like LINT), but I don't know if that's the reason for it here.
                    Silencing compiler warnings seems more likely.

                    -Kevin
                    --
                    My email address is valid, but changes periodically.
                    To contact me please use the address from a recent posting.

                    Comment

                    • Jacek Dziedzic

                      #11
                      Re: Cast to void???

                      This might be used to kill the compiler warning "unused variable
                      val" when the assert() is switched off (in a release compilation).
                      I used to use things like

                      val=val;
                      or
                      if(0) val=val;

                      for this purpose.

                      HTH,
                      - J.


                      Comment

                      Working...