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.)
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
Some editions of VisualStudio come packaged with a static analysis tool that used to be called PREFast (Now called simply "Code Analysis for C/C++"). PREFast uses annotations to mark up code. One of those annotations, MustCheck, does what you're looking for.
As far as I'm aware, the MS compilers don't have an equivalent pragma or attribute - the only "unused" type warning you can get is for variables when you have the optimizer turned on with the appropriate warning level.
It's
_Check_return_
. See here for examples of similar annotations and here for function behaviour. It's supported since MSVC 2012.Example:
UPDATE FOR MSVC 2012 AND LATER
Many thanks to @Albert for pointing out that MSVC now supports the annotation
_Check_return_
as of Visual Studio 2012 when using SAL static code analysis. I'm adding this answer so that I can include a cross-platform macro which may be useful to others:Note that, unlike gcc et al, (a) MSVC requires annotations on both declaration and definition of a function, and (b) the annotation needs to be at the start of the declaration/definition (gcc allows either). So usage will typically need to be e.g.:
Note also that you'll need the
/analyze
(or-analyze
) switch if compiling from the command line, or the equivalent if using the Visual Studio IDE. This also tends to slow the build down somewhat.I think the SAL annotation which others have mentioned is the right answer for MSVC, but I'm guessing some people will be interested in more portability than just MSVC, GCC, and GCC-compatible compliers, so…
First off, GCC only supports
warn_unused_result
since 3.4. You may want to check the values of__GNUC__
/__GNUC_MINOR__
instead of just checking if__GNUC__
is defined, although at this point I have trouble imagining anyone using a version of GCC older than 3.4.Several compilers support the GCC-style function attribute, and may or may not define
__GNUC__
and friends:__has_attribute(warn_unused_result)
), and compilers based on it (emscripten, xlc 13+, armclang, etc.), though AFAIK it always masquerades as at least GCC 4.2, so you probably don't need an explicit check.__GNUC__
(see the-no-gcc
flag). I don't know when they started supporting it (their documentation is severely lacking), but I know 16.0+ is safe.__TI_GNU_ATTRIBUTE_SUPPORT__
will be defined when it is.Additionally, C++17 adds a
[[nodiscard]]
attribute. For versions of GCC/clang which support[[nodiscard]]
in C++17 mode you can also use[[gnu::nodiscard]]
in C++11 and greater mode, but if you're hiding it behind a macro anyways I don't see a reason to do so instead of just using__attribute__((__warn_unused_result__))
.Putting it together, there is a HEDLEY_WARN_UNUSED_RESULT macro in Hedley which looks like:
You should be able to strip out the internal Hedley macros and just copy the logic without too much trouble if you don't want to use Hedley (it's public domain / CC0). If you choose to do so you should probably base your port off the version in the repo as I'm far less likely to remember to keep this answer up to date with new information.