0

I am new to C++ and am getting a compiler warning that I am not sure how to address. When I compile INBAND1->RasterIO(GF_Read, 0, y, xsize, 1, agc_data, xsize, 1, GDT_Float32, 0, 0); with c++ calc_emissions.cpp -o calc_emissions.exe -lgdal I get the warning

/usr/local/app/emissions/cpp_util/calc_gross_emissions_generic.cpp: In function 'int main(int, char**)':
/usr/local/app/emissions/cpp_util/calc_gross_emissions_generic.cpp:382:18: warning: ignoring return value of 'CPLErr GDALRasterBand::RasterIO(GDALRWFlag, int, int, int, int, void*, int, int, GDALDataType, GSpacing, GSpacing, GDALRasterIOExtraArg*)', declared with attribute warn_unused_result [-Wunused-result]
 INBAND1->RasterIO(GF_Read, 0, y, xsize, 1, agc_data, xsize, 1, GDT_Float32, 0, 0);
 ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

My code runs fine with the warning, but I'd like to suppress or resolve the warning. My understanding is that the warning is due to RasterIO() not having a return value (based on this). To suppress the warning, I tried INBAND1->(void)!RasterIO(GF_Read, 0, y, xsize, 1, agc_data, xsize, 1, GDT_Float32, 0, 0); as suggested here. However, that produces the following error

/usr/local/app/emissions/cpp_util/calc_gross_emissions_generic.cpp: In function 'int main(int, char**)':
/usr/local/app/emissions/cpp_util/calc_gross_emissions_generic.cpp:382:10: error: expected unqualified-id before '(' token
 INBAND1->(void)!RasterIO(GF_Read, 0, y, xsize, 1, agc_data, xsize, 1, GDT_Float32, 0, 0);
          ^
/usr/local/app/emissions/cpp_util/calc_gross_emissions_generic.cpp:382:11: error: expected primary-expression before 'void'
 INBAND1->(void)!RasterIO(GF_Read, 0, y, xsize, 1, agc_data, xsize, 1, GDT_Float32, 0, 0);
           ^~~~

My impression is that adding (void)! (or (void)) doesn't work here because of the INBAND1-> part of the line. Any suggestions for how to handle this warning? Thanks.

DGibbs
  • 13
  • 3
  • You have the `(void)!` on the wrong side of the `INBAND1->`. And may need to `(`...`)` the expression. (The `!` is to tell the compiler you've looked at it.) Alternatively, you could *use* the result and make sure it has a good result. – Eljay May 27 '22 at 20:50
  • 1
    `INBAND1->RasterIO( ... )` does have a return result - perhaps you should be checking it (there could be an error) ? – Richard Critten May 27 '22 at 20:52
  • 1
    @KamilCuk Because the linked answer explains that GCC purposefully ignores just `(void)` in this situation. – user17732522 May 27 '22 at 20:53

1 Answers1

1

how to handle this warning?

Handle the error

CPLErr errcode = INBAND1->RasterIO(GF_Read, 0, y, xsize, 1, agc_data, xsize, 1, GDT_Float32, 0, 0);
if (errcode != 0) {
    std::err << "och no rasterIO failed!\n";
    std::exit(1);
}

how to silence the compiler?

Put (void) in front of the line.

(void)INBAND1->RasterIO(GF_Read, 0, y, xsize, 1, agc_data, xsize, 1, GDT_Float32, 0, 0);
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • I've noticed that `(void)function(...);` isn't always successful and have sometimes had to do `auto dummy = function(...); static_cast(dummy);` to get around it. Still, same principle. – Ted Lyngmo May 27 '22 at 21:03
  • No, never put `(void)` in front of a function with attribute warn_unused_result. Someone went to all the trouble of marking the result to not be ignored for a reason. Don't ignore it. GCC purposefully ignores `(void)` in such a case because it violates the whole reason for the attribute as user17732522 points out. – Goswin von Brederlow May 27 '22 at 23:43
  • Your error handling suggestions works great. Thanks very much. Definitely prefer that to the warning silencing, but I appreciate your including both. – DGibbs May 31 '22 at 17:12
  • I want to add that warning about unused results is usually done to ensure that the function implementer is conscious about the return value. Ignoring it is perfectly fine as long as you are sure about what you're doing. It forces a cast to void which is very easily picked out during a review process. – Kim Ulvik Jun 23 '23 at 12:36