我见过this https://stackoverflow.com/q/6031819/10147399关于模仿的问题__builtin_unreachable
在旧版本的 GCC 中。我的问题正是如此,但针对的是 Visual Studio (2019)。 Visual Studio 是否有类似的功能__builtin_unreachable
?是否可以效仿一下?
顺便说一句,之前std::无法访问() http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0627r3.pdf可用,您可以将其实现为独立于编译器的函数,这样您就不必定义任何宏:
#ifdef __GNUC__ // GCC 4.8+, Clang, Intel and other compilers compatible with GCC (-std=c++0x or above)
[[noreturn]] inline __attribute__((always_inline)) void unreachable() {__builtin_unreachable();}
#elif defined(_MSC_VER) // MSVC
[[noreturn]] __forceinline void unreachable() {__assume(false);}
#else // ???
inline void unreachable() {}
#endif
Usage:
int& g()
{
unreachable();
//no warning about a missing return statement
}
int foo();
int main()
{
int a = g();
foo(); //any compiler eliminates this call with -O1 so that there is no linker error about an undefined reference
return a+5;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)