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 )
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With