c++gccgcc-warning

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 )


Solution

  • 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.