Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignoring warning "-Wunused-result"

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 )

like image 864
laterSon Avatar asked Nov 13 '16 16:11

laterSon


People also ask

How do I ignore GCC warnings?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

How does GCC treat warning errors?

The warning is emitted only with --coverage enabled. By default, this warning is enabled and is treated as an error. -Wno-coverage-invalid-line-number can be used to disable the warning or -Wno-error=coverage-invalid-line-number can be used to disable the error.

Which option can be used to display compiler warnings?

You can use a #pragma warning directive to control the level of warning that's reported at compile time in specific source files. Warning pragma directives in source code are unaffected by the /w option.


1 Answers

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.

like image 119
textshell Avatar answered Sep 21 '22 07:09

textshell