C++11 是否定义了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

我需要知道在指定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
}

或无数其他类似方式:返回
bool
std::true\u type
(有条件地)的
constepr
函数。两种类型之一的
静态
变量。一个traits类,它接受一个枚举,该枚举列出了各种
#define
令牌等价物(
eNDEBUG
等),这些等价物可以专门用于它所支持的每一个令牌,如果没有这样的支持,它会生成错误。使用
typedef
而不是
使用
(如果您的编译器对使用的支持很差,我将介绍您的MSVC2013)。我相信还有其他的。

如果您只对
NDEBUG
感兴趣,这相当于测试
assert()
是否对其参数求值。在这种情况下,您可以使用:

void f() noexcept(noexcept(assert((throw true,true))))
{
    // ...
}

当然,这不一定是一种改进:)

有很多种方法,但由于编译器的缺陷,并非所有方法都有效。它是gcc:error
noexcept()'具有不同的异常说明符