MSVC 相当于 __attribute__ ((warn_unused_result))?

2021-12-22 00:00:00 gcc c visual-c++ c++ gcc-warning

我发现 __attribute__ ((warn_unused_result)) 作为一种鼓励开发人员不要忽略函数返回的错误代码的方法非常有用,但我需要它与 MSVC 一起工作,以及gcc 和 gcc 兼容的编译器,例如 ICC.Microsoft Visual Studio C/C++ 编译器是否具有等效机制?(到目前为止,我已经尝试在 MSDN 中涉水但没有任何运气.)

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

推荐答案

它是 _Check_return_.有关类似注释和 此处 了解函数行为.自 MSVC 2012 起支持.

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

示例:

_Check_return_
int my_return_must_be_checked() {
    return 42;
}

相关文章