473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5516

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& ActionAtScopeEn d;
//...
ActionAtScopeEn d action( mkAction( functor( receiver,
&Receiver::func tion ) ) );
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.c omwrote in message
news:11******** **************@ m36g2000hse.goo glegroups.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& ActionAtScopeEn d;
//...
ActionAtScopeEn d action( mkAction( functor( receiver,
&Receiver::func tion ) ) );
valueUsed( action );

//Scope ends - action happens automatically and compiler happy that
value was used.
Hmm.. couldn't you just:
ActionAtScopeEn d action( mkAction( functor( receiver,
&Receiver::func tion ) ) ); 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.c omwrote:
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
3727
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 them) But this generates a warning (gcc4) saying it's an unused variable. Is there a better way to do this?
7
3383
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
1196
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
9629
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 Try/Catch, and the Catch for each one references the ex exception variable. Yet it is still flagged as unused? What gives?
0
9576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.