This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Any way to remove unreferenced parameter warnings?

We have a design that uses function pointers and sometimes the parameters passed to a "C" routine are not used. Thus we get alot of compiler warnings. Is there any way to remove these warnings? Previous versions of the compiler did not raise these warnings.

Parents
  • This is an eternal problem - especially in the type of situation you describe.

    There are various tricks, none of which is universally applicable to all compilers. :-(

    eg

    void fn( int unused1, unused2 )
    {
       unused1; // dummy reference to unused1
    
       unused2 = unused2; // dummy reference to unused2
    
    }
    Trouble is, some compilers will give warnings like "code with no effect" on these type of tricks!

    What we need is a #pragma to tell the compiler that a parameter is deliberately unused.
    The compiler would then generate an error if a parameter marked as "unused" was actually used!

Reply
  • This is an eternal problem - especially in the type of situation you describe.

    There are various tricks, none of which is universally applicable to all compilers. :-(

    eg

    void fn( int unused1, unused2 )
    {
       unused1; // dummy reference to unused1
    
       unused2 = unused2; // dummy reference to unused2
    
    }
    Trouble is, some compilers will give warnings like "code with no effect" on these type of tricks!

    What we need is a #pragma to tell the compiler that a parameter is deliberately unused.
    The compiler would then generate an error if a parameter marked as "unused" was actually used!

Children
  • I recommend defining a macro so that you can change implementation as needed between compiler versions or different platforms.


    #define UNUSED(arg)  if(arg){}
    
    void f (int a)
        {
        UNUSED(a);
        }
    

    (Note there's another implementation option shown. The idea is to find some syntactically legal statement with no side effects that will get optimized out, yet not produce any compiler or lint warnings.)

    Some compilers will stop complaining if you don't name the unused arguments:

    void f (int a, int /*b*/)
        {
        }