Robin Hsu
Robin Hsu

Reputation: 4484

Debug code and C++ unused parameter warning

I have search the solutions for C/C+ unused variable warning, and none fits my need. Here's the situation:

I am developing algorithms on PC, and will then port it to the real embedded platform. The PC version will have debug codes showing images on a monitor of a PC. The embedded platform, unfortunately, does not have a monitor. Of course the embedded platform does not need those debug code at all.

To distinguish the code, we can do this:

#ifndef EMBEDED
   MyDebugCode(parameter1, parameter2, parameter3);
#endif

However, I decide that this is too lousy, since there are so many calls of those debug functions. Those EMBEDED conditionals will pollute the code. Instead, I try to write the conditional compile flag inside the debug function like below:

inline void MyDebugCode(type1 p1, type2 p2, type3 p3)
{
#ifndef EMBEDED
     DisplaySomethingOnMonitor(p1, p2, p3);
     ...
     ...
     ...
#endif
}

So that I can write the #ifndef/#ifdef once, in the function, and call it many times without those #ifndef/#ifdef.

Now when EMBEDED is defined, we will get unused varaible p1, p2, and p3. This is the problem.

In addition, I am hoping the inline can be totally optimized out the whole debug function, as if it does not exist, when EMBEDDED is defined. This is secondary though.

Anyone has better solutions/practices?

Please note that those parameters can be references to class instances.

The compiler is g++

Upvotes: 3

Views: 761

Answers (2)

If your compiler has no other way to disable the warning, this should work:

#ifdef EMBEDED
inline void MyDebugCode(type1, type2, type3) {}
#else
inline void MyDebugCode(type1 p1, type2 p2, type3 p3)
{
    DisplaySomethingOnMonitor(p1, p2, p3);
    ...
    ...
    ...
}
#endif

Any reasonable compiler shouldn't warn on unused unnamed parameters, because there is no way to use them.

Upvotes: 1

rici
rici

Reputation: 241741

One possibly inelegant but common way to do this is to make MyDebugCode a macro which expands to nothing if it is undesired. A simple case would be:

#ifdef EMBEDDED
#  define MyDebugCode(a,b,c)
#else
inline void MyDebugCode(type1 p1, type2 p2, type3 p3)
{
     DisplaySomethingOnMonitor(p1, p2, p3);
     ...
     ...
     ...
}
#endif

(Some people would prefer to make MyDebugCode always a macro, either doing nothing or invoking a function with possibly a different name. YMMV.)

Upvotes: 1

Related Questions