pretendToUse.h,
which defines a function template that
allows the user to circumvent compiler warnings about an unused variable,
at zero runtime cost.
These packages may be obtained from the following sources:
fsgi01.fnal.gov),
in directories rooted at
/afs/fnal.gov/files/reports/working-groups/fpcltf/.
Certain design patterns make use of variables whose mere instantiation accomplishes some goal. However, compilers may detect an instantiated and apparently unused variable and warn that this may indicate a mistake.
In cases where the developer knows that an instantiated variable will not subsequently be used within its scope, it is useful to have a way to supress that warning, but without the runtime cost of actually using the variable.
Because pretendToUse(T) is a template, it can deal with any type of object T. And because its body is inline and empty, "invoking" this function costs zero at runtime.
sentry is declared but never used.
#include "myclasses/X.h"
#include "mytools/Cleanup.h"
#include "ZMtools/pretendToUse.h"
X::method() {
Cleanup sentry; // The destructor of sentry will trigger cleanup
pretendToUse(sentry);
// ... remainder of implementation
}
Another case of some interest is that of an overloaded function. The example code below shows how to deal with that.
Suppose we have a function named myfun overloaded so as to accept either a float or a double argument, and returning void. For some legitimate reason, neither signature is being used. Then the following code fragment will inhibit the usual compiler warnings.
{ void (*p)(float) = myfun;
pretendToUse(p); }
{ void (*p)(double) = myfun;
pretendToUse(p); }
A bit of explanation is in order. Read the first line pair as "Declare p
as a pointer to a function, myfun(float), which returns void and
pretend to use this function." Then the second line pair says "Declare p
as a pointer to a function, myfun(double), which returns void and
pretend to use this function."
The curly braces around each line pair defines a scope for p so that we can use the same symbol in both cases.