我需要知道NDEBUG是否是在指定to以外说明符时定义的。我在想,按照这个警察的职能:
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
}标准库或语言是否已经为此提供了一个工具,这样我就不会重新发明轮子了吗?
发布于 2015-03-19 16:52:25
如果您只对NDEBUG感兴趣,这相当于测试assert()是否评估它的参数。在这种情况下,您可以使用:
void f() noexcept(noexcept(assert((throw true,true))))
{
// ...
}当然,这并不一定是一种改进:)
发布于 2015-03-19 13:54:38
只需使用#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令牌等效物(eNDEBUG等),这些标记可以针对它支持的每个令牌进行专门化,如果没有这种支持,则生成错误。使用typedef而不是using (如果您的编译器对使用有片状的支持,我将查看MSVC2013)。我相信还有其他人。
https://stackoverflow.com/questions/29142256
复制相似问题