Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. UNUSED functions

UNUSED functions

Scheduled Pinned Locked Moved The Weird and The Wonderful
helptutorialquestion
12 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Maximilien

    this is a common practice. you can do either one of the following :

    void MyClass::MyMethod( int i, CString& s )
    {
    UNUSED( i );
    UNUSED( s );
    ///....
    }

    or

    void MyClass::MyMethod( int /*i*/, CString& /*s*/ )
    {
    ///....
    }

    or

    void MyClass::MyMethod( int, CString& )
    {
    ///....
    }

    Maximilien Lincourt Your Head A Splode - Strong Bad

    D Offline
    D Offline
    Delphi4ever
    wrote on last edited by
    #3

    I'm sure it's "common practice", but why have unused parameters in the first place? It's aint right, I tells ya! :confused:

    B C M J D 5 Replies Last reply
    0
    • D Delphi4ever

      I'm sure it's "common practice", but why have unused parameters in the first place? It's aint right, I tells ya! :confused:

      B Offline
      B Offline
      BadKarma
      wrote on last edited by
      #4

      This is not correct. When using a given framework, one can't choose the functions signature. Sometimes we only need to know when an event has occured, but extra information is given which we don't need. You could hide all warnings with the #pragma directive but that is dangerous, because that will hide alsoo the correct warnings. Therefore you should remove/comment the parameter from the function signature or use the parameter. Most compiler will ignore the a=a line when builded so there is no speed constaint. I preffer to comment the unused paramters out

      codito ergo sum

      C 1 Reply Last reply
      0
      • D Delphi4ever

        I'm sure it's "common practice", but why have unused parameters in the first place? It's aint right, I tells ya! :confused:

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #5

        Automatically VS2005 generated code:

        int APIENTRY _tWinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPTSTR lpCmdLine,
        int nCmdShow)
        {
        UNREFERENCED_PARAMETER(hPrevInstance);
        UNREFERENCED_PARAMETER(lpCmdLine);

        Actually it is just deceiving compiler to avoid warnings. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        [my articles]

        1 Reply Last reply
        0
        • B BadKarma

          This is not correct. When using a given framework, one can't choose the functions signature. Sometimes we only need to know when an event has occured, but extra information is given which we don't need. You could hide all warnings with the #pragma directive but that is dangerous, because that will hide alsoo the correct warnings. Therefore you should remove/comment the parameter from the function signature or use the parameter. Most compiler will ignore the a=a line when builded so there is no speed constaint. I preffer to comment the unused paramters out

          codito ergo sum

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #6

          BadKarma wrote:

          Therefore you should remove/comment the parameter from the function signature or use the parameter.

          You can also get the harmless warnings. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          [my articles]

          D 1 Reply Last reply
          0
          • D Delphi4ever

            I'm sure it's "common practice", but why have unused parameters in the first place? It's aint right, I tells ya! :confused:

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #7

            for example, in MFC : you have a message handler for the Left button down : afx_msg void OnLButtonDown(UINT nFlags, CPoint point); In my code ( or yours ) you might not need to use the nFlags parameter, so you do something like :

            void CMyWindow::OnLButtonDown(UINT /*nFlags*/, CPoint point)
            {

            }

            Maximilien Lincourt Your Head A Splode - Strong Bad

            1 Reply Last reply
            0
            • D Delphi4ever

              I'm sure it's "common practice", but why have unused parameters in the first place? It's aint right, I tells ya! :confused:

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #8

              The only reason it may be "common practice" is a policy of no warnings or to eliminate irritating warnings about something that does no harm. I have never seen it done, as it is a method/function signature thing and commenting out the parameters makes more since (aka: /*parameter*/). On the plus side, modern compilers will normally optimize out self assignment, so using such macros eliminates the warning with out producing uneeded code (well it is supposed to anyway). The down side is wasting time typing in bull<blank> code that has nothing to do with what the method/function does. It is interesting, but I do not like the idea at all. :doh:

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              1 Reply Last reply
              0
              • D Delphi4ever

                Not very horrific, but I think it is kind of cute: #define UNUSED1(a) a=a #define UNUSED2(a,b) a=a,b=b #define UNUSED3(a,b,c) a=a,b=b,c=c #define UNUSED4(a,b,c,d) a=a,b=b,c=c,d=d What use could these UNUSED functions possibly have? Well, I had to ask one of the other programmers. If you have unused parameters in a functions parameter list, a warning occurs "unused parameters in the parameter list" or something like that. How to get rid of those pesky unused parameters? Call the UNUSED function with those unused parameters. The UNUSED function will use the unused parameters, making them used parameters instead. Now no warning for unused parameters will occur! Problem solved! :-D

                C Offline
                C Offline
                Cristian Amarie
                wrote on last edited by
                #9

                It is horrific indeed.

                #define UNUSED(a) a=a

                could suffer from side effects caused by the assignment operator. Consider

                int x = 3;
                UNUSED(++x);
                return x;

                Which will be the value of x? Or it could be a function that returns an unused variable, such as

                int& f(int& x) {
                return ++x;
                }
                main() {
                int x = 2, y;
                UNUSED(y = f(x), y); // y == x == 3
                }

                and we end up in

                int& f(int& x) {
                return ++x;
                }
                main() {
                int x = 2, y;
                (y = f(x), y) = (y = f(x), y); // y == x == 4
                }

                UNUSED should mean one will not use the parameter in subsequent operations, but won't alter the content, nor produce side-effects - so let evaluate it but don't use the result. UNREFERENCED_PARAMETER(a) or even ((void)(a)) will do here. Those UNUSED are really pain in the a** here.

                Nuclear launch detected

                M 1 Reply Last reply
                0
                • D Delphi4ever

                  I'm sure it's "common practice", but why have unused parameters in the first place? It's aint right, I tells ya! :confused:

                  D Offline
                  D Offline
                  Dan Neely
                  wrote on last edited by
                  #10

                  Because the parameters are part of a published and widely used API, and as a result redefining the signature isn't an option even if the parameter is no longer needed?

                  Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

                  1 Reply Last reply
                  0
                  • C Cristian Amarie

                    It is horrific indeed.

                    #define UNUSED(a) a=a

                    could suffer from side effects caused by the assignment operator. Consider

                    int x = 3;
                    UNUSED(++x);
                    return x;

                    Which will be the value of x? Or it could be a function that returns an unused variable, such as

                    int& f(int& x) {
                    return ++x;
                    }
                    main() {
                    int x = 2, y;
                    UNUSED(y = f(x), y); // y == x == 3
                    }

                    and we end up in

                    int& f(int& x) {
                    return ++x;
                    }
                    main() {
                    int x = 2, y;
                    (y = f(x), y) = (y = f(x), y); // y == x == 4
                    }

                    UNUSED should mean one will not use the parameter in subsequent operations, but won't alter the content, nor produce side-effects - so let evaluate it but don't use the result. UNREFERENCED_PARAMETER(a) or even ((void)(a)) will do here. Those UNUSED are really pain in the a** here.

                    Nuclear launch detected

                    M Offline
                    M Offline
                    Mike Diack
                    wrote on last edited by
                    #11

                    The cleanest way I've found which compiles with both /W4 and cleanly with lint, is simply to cast the variables to void, like so (C example, but works with C++) int myfunction (int unusedarg) { (void) unusedarg; return (1); } Mike

                    1 Reply Last reply
                    0
                    • C CPallini

                      BadKarma wrote:

                      Therefore you should remove/comment the parameter from the function signature or use the parameter.

                      You can also get the harmless warnings. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      [my articles]

                      D Offline
                      D Offline
                      DRHuff
                      wrote on last edited by
                      #12

                      Ahh just start ignoring those harmless warnings... That way lies madness!

                      I'm pretty sure I would not like to live in a world in which I would never be offended. I am absolutely certain I don't want to live in a world in which you would never be offended. Dave

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups