开发者

How to suppress individual warnings in C++?

开发者 https://www.devze.com 2022-12-19 07:24 出处:网络
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.

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?


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;
}


#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 ;)


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.


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 ){


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消