我需要知道在指定 noexcept 说明符时是否定义了 NDEBUG。我在考虑这个 constexpr 函数:
constexpr inline bool is_defined() noexcept
{
return false;
}
constexpr inline bool is_defined(int) noexcept
{
return true;
}
然后像这样使用它:
void f() noexcept(is_defined(NDEBUG))
{
// blah, blah
}
标准库或语言是否已经为此提供了便利,这样我就不会重新发明轮子了?
最佳答案
只用#ifdef
?
#ifdef NDEBUG
using is_ndebug = std::true_type;
#else
using is_ndebug = std::false_type;
#endif
void f() noexcept(is_ndebug{}) {
// blah, blah
}
或无数其他类似的方式:constexpr
函数返回 bool
或 std::true_type
(有条件地)。两种类型之一的 static
变量。一个特征类,它采用一个枚举,其中列出了各种 #define
token 等价物(eNDEBUG
等),这些 token 可以专门用于它支持的每个此类 token ,并在以下情况下生成错误没有这样的支持。使用 typedef
而不是 using
(如果你的编译器对 using 的支持不稳定,我正在看你的 MSVC2013)。我敢肯定还有其他人。
关于c++11 - is_defined constexpr 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142256/