Friday 27 December 2019

c++ - MSVC equivalent of __attribute__ ((warn_unused_result))?



I'm finding __attribute__ ((warn_unused_result)) to be very useful as a means of encouraging developers not to ignore error codes returned by functions, but I need this to work with MSVC as well as gcc and gcc-compatible compilers such as ICC. Do the Microsoft Visual Studio C/C++ compilers have an equivalent mechanism ? (I've tried wading through MSDN without any luck so far.)


Answer



It's _Check_return_. See here for examples of similar annotations and here for function behaviour. It's supported since MSVC 2012.



Example:




_Check_return_
int my_return_must_be_checked() {
return 42;
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...