views:

214

answers:

6

First of all, sorry if this is an obvious question, but I'm rather new to C++. Also, this code is not originally mine, but I am trying to clean it up.


I'm looking for a compiler independent way to surpress warnings (preferably) for a specific line. I've got the following code:

int MPtag::state_next( int i, int s ){
#if NGRAMS==2
    return s+1;
#elif NGRAMS==3
    return tag_at(i,0) * num_tags + s+1;
#elif NGRAMS>=4
    return tag_at(i,-1) * num_tags*num_tags + tag_at(i,0)*num_tags + s+1;
#endif
}

NGRAMS is currently set to 2.

G++ gives me a warning (with the appropriately paranoid options of course) that the parameter "i" is unused. While this is technically true, it is not always the case. I've thought about commenting out the variable name, but then if NGRAMS were to be changed it would produce a compiler error until uncommented; which is undesirable.

The oldest answer for related question proposes a macro, but another poster say that it is not compiler independent. I've read about #pragma warning but AFAICT that is VS C++ thing. Is there even a proper way to do this?

A: 

There is no standard way of supressing warnings as warnings are compiler dependants.

You'll have to use compiler specific #pragma, or make sure your code don't generate any warning on the different compilers, or just make sure it don't generate warnings on the main compiler you're working with and don't bother with others.

Klaim
+2  A: 
#if NGRAMS==2
int MPtag::state_next( int, int s ){
    return s+1;
#else
int MPtag::state_next( int i, int s ){
#if NGRAMS==3
    return tag_at(i,0) * num_tags + s+1;
#elif NGRAMS>=4
    return tag_at(i,-1) * num_tags*num_tags + tag_at(i,0)*num_tags + s+1;
#endif
#endif
}

That'll suppress your warning ;)

Goz
+4  A: 

For that particular warning you can always cheat:

#define UNREFERENCED_PARAMETER( x ) ( x )

Then in your code

int a( int b, int c )
{
    UNREFERENCED_PARAMETER( c );
    return b * b;
}

As a plus, the UNREFERENCED_PARAMETER macro definition sort of looks like boobies on the end.

taspeotis
I use `#define UNUSED_VAR(x) (x)=(x)` which would not make the compiler complain about the statement having no effect
Hasturkun
`(void)c; // will solve it too`
LiraNuna
A: 

There is no compiler independent way i know of.
A simple solution would be to wrap the

int MPtag::state_next( int i, int s ){

into #ifdef's, too.

dbemerlin
+1  A: 

The standard C++ way would be:

#if NGRAMS==2
int MPtag::state_next( int /*i*/, int s ){
...
#else

Note this does not work with C. Further, for C, GCC has the unused attribute. This however doesn't work with C++ code (needs to be fixed).

int foo( int __attribute__((__unused__)) i, int s ){
dirkgently
A: 

The easiest way is of course to make the parameter disappear when not needed, like so:

int MPtag::state_next( int
#if NGRAMS != 2
  i
#endif
, int s )
{
#if NGRAMS==2
    return s+1;
#elif NGRAMS==3
    return tag_at(i,0) * num_tags + s+1;
#elif NGRAMS>=4
    return tag_at(i,-1) * num_tags*num_tags + tag_at(i,0)*num_tags + s+1;
#endif
}

This repeats the "knowledge" that i is not needed when NGRAMS is two, but I think it's good enough for such a tiny and highly-localized case.

unwind
While some of the other ways are perhaps "more proper" they should be done once in a common header to all files, which i currently do not have etc... I like this answer because its a nice for a quick'n'dirty one off.
ArtB