11

I am new to c++.

I want to ignore warning -Wunused-result which I guess popped because of -Wall flag.

I did search on net and found that this is I can ignore it by declaring a pragma. I don't have much knowledge about pragma but it says that I have to write warning number in order to ignore it.

What is warning number of -Wunused-result, or is there any other way I can ignore or disable this specific warning?

code:-

freopen("input", "r", stdin);
freopen("output", "a", stdout);

on compiling:-

warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]

I found that I need to declare something like


#pragma warning( disable : number_of_warning )

laterSon
  • 183
  • 1
  • 2
  • 14
  • 4
    Pro-tip: you generally don't want to *ignore* warnings. You want to *fix* them. In most cases they point to real problems in your code that you want to handle. – Jesper Juhl Nov 13 '16 at 17:04

3 Answers3

20

As the other answers say these warnings are usually for a good reason.

But if you need to suppress warnings caused by __attribute__ ((__warn_unused_result__)) in gcc the usual simple cast to void does not work.

What does work is:

(void)!freopen("input", "r", stdin);

That (void) alone isn't enough is on purpose according to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 The workaround is from comment 34 in that bug report.

textshell
  • 1,746
  • 14
  • 21
  • 1
    I suggest putting the workaround from comment 34 into your answer so it is self-contained. – Scott McPeak Jun 06 '21 at 22:15
  • 1
    The example code in my answer is using exactly that workaround. The link is just the link to the source where i got that workaround. – textshell Jun 08 '21 at 16:41
  • Oops, sorry! I did not see the `!` in your answer; I only saw it when I clicked through. That's my fault; I read it too fast. But I notice that the example at the link has a helpful comment pointing to the `!`. Perhaps you could add that to your answer as well? – Scott McPeak Jun 08 '21 at 17:48
  • This works in modern gcc. Very handy for ignoring some return values without turning off unused warnings completely. Thanks! – CR. Aug 31 '21 at 05:18
8

If the return value of a function is to ignored, then one portable-ish way is to mark it with void as:

  (void) frepoen("input", "r", stdin);

It is a clear indication to both the reader as well as the compiler that the return value is really not necessary.

However, if a file is re-opened (freopen), then isn't the return value (FILE *) necessary for subsequent read/write operations on the file?

As Striezel pointed out, for stdin and stdout, althught the return value is not necessary for subsequent file operation, it may still be necessary for error checking. Upon failure, freopen returns NULL.

Arun
  • 19,750
  • 10
  • 51
  • 60
  • 16
    I am using g++ 5.4, and casting the return value to void isn't helping. I still get the warning. – Dima Mar 08 '17 at 19:42
  • I suppose this answer may have helped the asker, but there should be an answer which is general to the question in the title. – Timothy Swan Mar 05 '18 at 22:17
  • This doesn't work on g++ (the compiler the OP asked about as its about c++ and gcc is tagged). I guess it works in pure C? – BjornW Jun 24 '21 at 21:44
3

You have several options:

  1. Just ignore the warning. Meaning, do nothing and just close your eyes whenever it pops up on your screen.

  2. Tell your compiler to not emit the warning (haven't checked this specific one, but in most cases you can pass -Wno-<warning-in-question> (or use a pragma).

  3. Cast the return value to void if you really want to ignore it (you probably don't).

  4. Fix the actual problem by actually using the variable/return value (probably what you actually want).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70