473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

unused variables...

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

Jul 5 '07 #1
9 5463

Zeppe wrote:
werasm wrote:
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?
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

Jul 5 '07 #2
werasm wrote:
Zeppe wrote:
>werasm wrote:
>>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?

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
Jul 5 '07 #3
werasm wrote:
>
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)
Jul 5 '07 #4
"werasm" <we****@gmail.comwrote in message
news:11**********************@m36g2000hse.googlegr oups.com...
>
Zeppe wrote:
>werasm wrote:
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?

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.
Jul 5 '07 #5

Pete Becker wrote:
werasm wrote:

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.
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

Jul 5 '07 #6

Jim Langston wrote:
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.

Jul 5 '07 #7
werasm wrote:
Pete Becker wrote:
>werasm wrote:
>>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.

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)
Jul 5 '07 #8
On Jul 5, 6:37 am, werasm <wer...@gmail.comwrote:
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

Jul 5 '07 #9

Puppet_Sock wrote:
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

Jul 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: artooro | last post by:
Hi, I'm using some lines of code like this: ClassName *var = new ClassName(...); and that's it. The class constructor does everything I want (there are methods to do more but I don't need...
7
by: ralphNOSPAM | last post by:
Is there a PHP script that can find unused variables? I'd like to 'clean up' my scripts. Thanks...
4
by: Rob T | last post by:
Is there a way to scan a project and find the variables that have been declared but never used? I'm fixing someone else's code and it filled with these things! Thanks.
12
by: zacks | last post by:
Suddenly, in a VB2005 project I am working on, several variables show up in the list of warnings are being unused local variables. But they all are. Several of them are the ex variable used in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.