C++11 是否定义了constexpr函数
我需要知道在指定noexcept说明符时是否定义了NDEBUG。我是按照constexpr函数的思路思考的:C++11 是否定义了constexpr函数,c++11,c++14,constexpr,C++11,C++14,Constexpr,我需要知道在指定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, bl
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
}
或无数其他类似方式:返回
bool
或std::true\u type
(有条件地)的constepr
函数。两种类型之一的静态变量。一个traits类,它接受一个枚举,该枚举列出了各种#define
令牌等价物(eNDEBUG
等),这些等价物可以专门用于它所支持的每一个令牌,如果没有这样的支持,它会生成错误。使用typedef
而不是使用
(如果您的编译器对使用的支持很差,我将介绍您的MSVC2013)。我相信还有其他的。如果您只对NDEBUG
感兴趣,这相当于测试assert()
是否对其参数求值。在这种情况下,您可以使用:
void f() noexcept(noexcept(assert((throw true,true))))
{
// ...
}
当然,这不一定是一种改进:)有很多种方法,但由于编译器的缺陷,并非所有方法都有效。它是gcc:errornoexcept()'具有不同的异常说明符