1

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?

Community
  • 1
  • 1
Sled
  • 18,541
  • 27
  • 119
  • 168
  • G++ supports some msvc extensions, you should try it `#pragma warning(disable:nnnn)` where nnnn is the warning number – John Knoeller Feb 04 '10 at 09:22
  • I had problems with this too. We have code here that compiles on different compilers (G++ 3.x for embedded, G++ 4.x for desktop, VC++9 for desktop/tests), and they all have different warnings. G++ has options to suppress _some_ warnings, while in VC++, every warning and error has a numerical code which can be suppressed with compiler options and pragmas. I was not satisfied with the situation. It gets worse if you have _treat warnings as errors_ enabled. – OregonGhost Feb 04 '10 at 09:24
  • @John, Where does one find the numbers for warnings? My compiler just gives me messages like "code/MPtag.C:68: warning: unused parameter ‘i’"? – Sled Feb 04 '10 at 10:13
  • 68 looks like the warning number to me. – John Knoeller Feb 04 '10 at 20:51

6 Answers6

7

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;
}
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • 2
    I use `#define UNUSED_VAR(x) (x)=(x)` which would not make the compiler complain about the statement having no effect – Hasturkun Feb 04 '10 at 09:28
3
#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
  • 61,365
  • 24
  • 124
  • 204
2

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
  • 391,730
  • 64
  • 469
  • 606
  • 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. – Sled Feb 16 '10 at 22:22
1

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
  • 108,024
  • 16
  • 131
  • 187
0

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
  • 67,274
  • 36
  • 133
  • 188
0

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.