Board index » cppbuilder » Re: bcb6 incorrectly warns 'declared but never used'

Re: bcb6 incorrectly warns 'declared but never used'


2004-01-15 08:01:33 AM
cppbuilder77
Quote
Thus nTimes is REALLY const. It is undefined to cast away constness
on variables that are created being const
You're right. That's why I wrote "Here's a {*word*193} one". :-) But
seriously, there's no other legitimate way to supress this warning but
to turn the 8080 compiler warning off. No matter how many times you try
to read that const value, the compiler won't shut up. Unfortunately the
ACE_UNUSED_ARG(a) didn't work. SHUT_UP_ALMOST_ANY_COMPILER(x) neither:
const int nTimes = 3;
(void) nTimes; // still there's a warning!
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}
It's a bug, and you can not silence the compiler in any other way but to
turn off the warning itself.
Tom
 
 

Re:Re: bcb6 incorrectly warns 'declared but never used'

Quote
It's a bug, and you can not silence the compiler in any other way but to
turn off the warning itself.
Or using static, which you have already pointed out that it might not be
zero-overhead.
Tom
 

Re:Re: bcb6 incorrectly warns 'declared but never used'

OK, finally I got it. It was really tough. Here's a solution that's not
going to crash, has 0 overhead in my opinion, and really works:
#pragma warn +8080
void f()
{
const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}
const int& rnTimes = nTimes;
(void) rnTimes;
}
So you can write a simple macro like this:
#define SHUT_UP_BORLAND(x) const int& temp__##x = x; (void) temp__##x;
You should only use this once within a scope.
Tom
Chris Uzdavinis (TeamB) wrote:
Quote
Philippe Allain < XXXX@XXXXX.COM >writes:


>Chris Uzdavinis (TeamB) wrote:
>
>>I'd suggest just adding a statement like this, like Alan suggested:
>>const int nTimes = 3;
>>(void) nTimes; /////<<<<<<<<<<< Just add this, it should noop.
>
>This doesn't work unfortunately.


Can you define what you mean by "doesn't work". Doesn't compile, or
doesn't suppress the error?

In the ACE library there is a macro called ACE_UNUSED_ARG which you
can use to silence such warnings. For example:

ACE_UNUSED_ARG(nTimes);

You don't want to use ace just for this macro but you can duplicate
what it does. On some platforms the (void) trick works nicely. I
thought borland was one of those platforms but I was writing from
memory. I'll look at ace to see how this macro is implemented for
borland... AH, here it is:

# define ACE_UNUSED_ARG(a) (a)

Hmm, looks pretty simple. I like this macro, BTW, rather than doing
whatever you do inside code to fix it, since this actually documents
WHAT you're trying to do.

 

{smallsort}

Re:Re: bcb6 incorrectly warns 'declared but never used'

Tamas Demjen < XXXX@XXXXX.COM >writes:
Quote
>Thus nTimes is REALLY const. It is undefined to cast away constness
>on variables that are created being const

You're right. That's why I wrote "Here's a {*word*193} one". :-) But
seriously, there's no other legitimate way to supress this warning but
to turn the 8080 compiler warning off. No matter how many times you
try to read that const value, the compiler won't shut
up. Unfortunately the ACE_UNUSED_ARG(a) didn't
work. SHUT_UP_ALMOST_ANY_COMPILER(x) neither:
Surprising. Thanks for the info, though.
Quote
const int nTimes = 3;
(void) nTimes; // still there's a warning!
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}
How about declaring nTimes in the anonymous namespace, instead of
making it local to the function?
--
Chris (TeamB);
 

Re:Re: bcb6 incorrectly warns 'declared but never used'

Quote
Surprising. Thanks for the info, though.
I was surprised too. The reason it works is because if we pass a pointer
or reference to the variable, the compiler believes the variable is
used. If you just try to use its value, the warning will always be issued.
Quote
How about declaring nTimes in the anonymous namespace, instead of
making it local to the function?
You mean this?
#pragma warn +8080
namespace { const int nTimes = 3; }
void f()
{
for(int k=0; k < nTimes; k++) ;
}
Good idea! It works too. No warning.
Tom