0

I got some code and trying to removing all warning for the given code.

While for certain function like:

sub_main(int /*argc*/, char** /*argv*/){

-------------
obj1* ptr1 = new obj1(xxx);
obj2* ptr2 = obj1->xxxx(xxxx)

-------------

}

While this ptr1 and ptr2 is not used in this function at all. (It should be used somewhere since if I comment them out,I got certain errors).

And there are some ways to slient the warning for unused parameters like

sub_main(int /*argc*/ )

or

sub_main(int a _ _ attribute_ _ ((unused)))

While for unused variables, is there any similiar neat way to slient the warning?

I don't want to do some ifdef for gcc to remove warning or silient the warning in makefile.

Thanks.

thundium
  • 995
  • 4
  • 12
  • 30
  • The common way to silence unused variable warnings is the statement `(void)unused;`. – Deduplicator May 13 '14 at 14:13
  • BTW: I hope you compile with `-Wall -Wextra`. So, how come the compiler accepts a function without return type? – Deduplicator May 13 '14 at 14:14
  • 3
    `ptr1` and `ptr2` are defined locally in `sub_main`. You say that if you comment them out, you get errors -- which implies that you *are* using them somewhere in `sub_main`. In C++, you cannot define a function without a return type (unless it's a constructor or destructor). Please update your question to show a complete self-contained example, along with the exact warning message you get from your compiler. Read this: http://sscce.org/ – Keith Thompson May 13 '14 at 14:25
  • `obj1->xxxx`? Really? – Lightness Races in Orbit May 13 '14 at 14:51

4 Answers4

1

for an unused variable var you can use:

(void)var;
nh_
  • 2,211
  • 14
  • 24
  • According to http://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/ that (may) fails on on EDG-based compilers. – Jarod42 May 13 '14 at 14:59
0

Firstly, why did you comment out argc and argv. If you don't plan to use them they are not needed. You could simply

sub_main()

and that would be sufficient, but if you plan on using them, regarless of when you should include them. Somehow this looks like K&R/ANSI C though, if you were using C++98+, you should define them like so:

int sub_main(int argc, char ** const argv)
{
    ...
    return 0; //or whatever
}

Valid K&R/ANSI syntax would be

sub_main(argc, argv) //int left off as it defaults to int
int argc;
char ** const argv;
{
    ...
    return 0; //or whatever
}

Also, the unused variable warnings partially came from your commenting of parameter names (C/C++ tends to ignore unused parameters, but likes to get fussy about unnamed parameters, though C++99 onwards it is valid, and required for some functions, say postfix operators.)

The other issue is that, you are never using the variables ptr1 and ptr2. As soon as you use them, the warning will go away. If you don't plan to use them at all, they should not be defined as it is usually a waste of memory (though it probably dosn't matter at the moment c++ still is set up to yell).

  • The question is about C++, which has never supported old-style function definitions, so there's no point in mentioning them. – Keith Thompson May 13 '14 at 14:22
0

When you encounter a warning, your first (and second, and probably third) step should be to try to remove it by solving it. Only once you've genuinely determined it's bogus should you turn to silencing it.

In your case, if ptr1 is not used at all, don't declare it. You said that removing it introduces errors elsewhere. This implies obj1 does crazy stuff in its constructor (like registering the newly created instance somewhere), or the whole code is a mess. In case it's the former, simply drop the variable definition and just do new obj1(xxx); as a statment. But first make triple sure that the pointer gets stored somewhere, and document that by a comment next to the new expression.

The line with ptr2 shouldn't even compile as it is, since obj1 is a type and you can't apply -> to it. But I assume you actually meant ptr1->xxxx(xxx), in which case ptr1 is indeed used and only ptr2 needs removing - simply leave ptr1 as is and just replace the declaration of ptr2 by the call ptr1->xxxx(xxx);. And make sure the object pointed to by ptr1 is not leaked.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

A way to avoid this warning is to define a function like:

template <typename T> remove_warning_for_unused_variable(const T&) {}

And then use it

remove_warning_for_unused_variable(variable);
Jarod42
  • 203,559
  • 14
  • 181
  • 302