unused variables...

W

werasm

Hi all,

I'm looking for a nice clean (portable) way to get rid of unused
variable warnings without fiddling with compiler settings (on a per
case basis). I've come up with this:

template <class T>
inline void valueUsed( const T& ){}

int main()
{
int v( 0 );
const int& x( v );
int& y( v );
valueUsed( v );
valueUsed( x );
valueUsed( y );
}

Any foreseen pitfalls with this solution?

Regards,

Werner
 
Z

Zeppe

werasm said:
Hi all,

I'm looking for a nice clean (portable) way to get rid of unused
variable warnings without fiddling with compiler settings (on a per
case basis). I've come up with this:

template <class T>
inline void valueUsed( const T& ){}

Do you mean removing the variable name from the function declaration?
It's ok, I think it's the common solution to this problem. Often one
just comment out the name, so the variable name stays there for
documentation (in accordance with virtual member functions, for example,
or overloaded ones).

The only drawback I'm aware of is that in C it doesn't work, because,
being the functions not overloadable, it makes no sense to declare an
input variable without any name.

Regards,

Zeppe
 
W

werasm

Zeppe said:
Do you mean removing the variable name from the function declaration?

No. I have a utility called action at scope end that basically calls a
functor at scope end. I usually bind it to a reference that goes out
of scope at the end of a function (as it is not copyable). It works
well, but has the problem that it riddles my build with "unused
variable" errors unnecessarily. This is what I came up with to make
the problem go away - like:

typedef const Action& ActionAtScopeEnd;
//...
ActionAtScopeEnd action( mkAction( functor( receiver,
&Receiver::function ) ) );
valueUsed( action );

//Scope ends - action happens automatically and compiler happy that
value was used.

Regards,

Werner
 
Z

Zeppe

werasm said:
No. I have a utility called action at scope end that basically calls a
functor at scope end. I usually bind it to a reference that goes out
of scope at the end of a function (as it is not copyable). It works
well, but has the problem that it riddles my build with "unused
variable" errors unnecessarily. This is what I came up with to make
the problem go away - like:

I see, and I had the same problem some time ago. The variable that you
are creating is called a "guard". The solution seems fine to me, I think
that none of the compilers can be so stupid to put actually some code
for the function isUsed, but the standard doesn't guarantee that.

Regards,

Zeppe
 
W

werasm

Zeppe said:
Do you mean removing the variable name from the function declaration?

I realise this may be a solution for that too, so yes, but that was
not my original intent:

What you are saying is:

void foo( int arg )
{
valueUsed( arg ); //Ok, good for this too...
}
In the case of builtin types keeping the name may suffice for
documentation, else the type name (or class name) is often
documentation enough.

Thanks for your response.

W
 
P

Pete Becker

werasm said:
I'm looking for a nice clean (portable) way to get rid of unused
variable warnings without fiddling with compiler settings (on a per
case basis).

In other words, you're going to complicate valid, meaningful code to
satisfy some compiler writer's notion of proper style. A simpler
approach is to turn off the warning.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
J

Jim Langston

werasm said:
No. I have a utility called action at scope end that basically calls a
functor at scope end. I usually bind it to a reference that goes out
of scope at the end of a function (as it is not copyable). It works
well, but has the problem that it riddles my build with "unused
variable" errors unnecessarily. This is what I came up with to make
the problem go away - like:

typedef const Action& ActionAtScopeEnd;
//...
ActionAtScopeEnd action( mkAction( functor( receiver,
&Receiver::function ) ) );
valueUsed( action );

//Scope ends - action happens automatically and compiler happy that
value was used.

Hmm.. couldn't you just:
ActionAtScopeEnd action( mkAction( functor( receiver,
&Receiver::function ) ) ); action;

action by itself without assignment, etc... is basically a non op, but it
should get rid of your unused variable warning.
 
W

werasm

Pete said:
In other words, you're going to complicate valid, meaningful code to
satisfy some compiler writer's notion of proper style. A simpler
approach is to turn off the warning.

The code most often exists in templates. This means the client code is
(or may be) riddled with warnings. Therefore this shifts the burden to
the client - not so nice.

W
 
W

werasm

Jim said:
action by itself without assignment, etc... is basically a non op, but it
should get rid of your unused variable warning.

Yes, it gives me another warning - expression has no effect. I would
like my code to compile clean when used by a client.
 
P

Pete Becker

werasm said:
The code most often exists in templates. This means the client code is
(or may be) riddled with warnings. Therefore this shifts the burden to
the client - not so nice.

Stupid warnings are no less stupid when they're generated in client
code. Turn off stupid warnings.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
P

Puppet_Sock

Hi all,

I'm looking for a nice clean (portable) way to get rid of unused
variable warnings without fiddling with compiler settings (on a per
case basis). I've come up with this:

template <class T>
inline void valueUsed( const T& ){}

int main()
{
int v( 0 );
const int& x( v );
int& y( v );
valueUsed( v );
valueUsed( x );
valueUsed( y );

}

Any foreseen pitfalls with this solution?

You don't need to do it for v. The variable v is
already referenced.

What would be wrong with just referencing the variable?
That is, what does the template do for you?

int main(int argc, char* argv[])
{
argc; // get rid of unrefed variable warning
argv; // get rid of unrefed variable warning
int v( 0 );
const int& x( v );
int& y( v );
x; // get rid of unrefed variable warning
y; // get rid of unrefed variable warning
return 0;
}

Socks
 
W

werasm

Puppet_Sock said:
You don't need to do it for v. The variable v is
already referenced.

Yes :), but the reference is not used. That is what the compiler is
jumping about.

I sometimes bind temporaries to reference to scope them, for example:

const int& i( 10 );

now the value 10 lives for as long as the enclosed scope, but i is not
used. This is a stupid example, but I have a class that calls a
function at scope end (its scope end), and I have a make that returns
a temporary that is bounded to a const reference.

Pete has a point, I suppose. Sometimes, though - I might want to know
about unused variables, so disabling the (compiler) options at project
level is too much for me. Also, it is tedious to disable the option
every time a piece of often used code is used. Perhaps options can be
disabled by pragmas or by comments, but to do that for various
compilers becomes more messy than just simply stating "verbosely" in
the code...

isUsed( x );

.... due to the fact that it is the exiting of scope that matters.

Admittely, > 95% percent of the time this warning is really stupid.

Regards,

Werner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,479
Members
48,143
Latest member
Manuelevimb

Latest Threads

Top