C++11 C++;11-如何修复无法使用noexcept说明符检测函数声明的noexcept运算符 我为个人研究目的建立了一个新的图书馆,我试图全面理解C++标准库工具和核心功能。现在我在理解noexcept操作符时遇到了一个问题

C++11 C++;11-如何修复无法使用noexcept说明符检测函数声明的noexcept运算符 我为个人研究目的建立了一个新的图书馆,我试图全面理解C++标准库工具和核心功能。现在我在理解noexcept操作符时遇到了一个问题,c++11,decltype,noexcept,C++11,Decltype,Noexcept,我编写了一些涉及noexcept运算符的测试示例,我对以下断言的结果感到困惑: ... void no_throw() noexcept; static_assert(noexcept(no_throw), "This should be true"); static_assert(noexcept((std::declval<decltype(no_throw)>())()), "this also should be true"); ... 。。。 void no_throw

我编写了一些涉及noexcept运算符的测试示例,我对以下断言的结果感到困惑:

...
void no_throw() noexcept;

static_assert(noexcept(no_throw), "This should be true");
static_assert(noexcept((std::declval<decltype(no_throw)>())()), "this also should be true");
...
。。。
void no_throw()无例外;
静态断言(noexcept(no-throw),“这应该是真的”);
静态断言(noexcept((std::declval())(),“这也应该是真的”);
...
我希望这段代码能够编译,但只有在使用c++17编译标志时,第二个断言才会通过;我确实用gcc8.1和clang5.0运行了测试。我没有用其他编译器测试过

使用c++11或c++14标志失败。有人能解释一下为什么吗


谢谢你的第一次测试,不要工作:这是通过

void no_throw() noexcept(true) {};
void do_throw() noexcept(false) {};

static_assert(noexcept(no_throw), "");
static_assert(noexcept(do_throw), "");
应该是:

void no_throw() noexcept(true) {};
void do_throw() noexcept(false) {};

static_assert(noexcept(no_throw()), "");
static_assert(!noexcept(do_throw()), "");
那么,为什么只有在17:

直到17:noexcept规范不是函数的一部分 类型 资料来源:

因此,如果您在11中运行此代码:

template<class T>
   struct TD;

TD<decltype(no_throw)> e;
TD<decltype(std::declval<decltype(no_throw)>())> e2;
模板
结构TD;
TD e;
TD-e2;
你得到:

prog.cc:14:24: error: aggregate 'TD<void()> e' has incomplete type and cannot be defined
 TD<decltype(no_throw)> e;
                        ^
prog.cc:15:50: error: aggregate 'TD<void (&)()> e2' has incomplete type and cannot be defined
 TD<decltype(std::declval<decltype(no_throw)>())> e2;
prog.cc:14:24:错误:聚合“TD e”的类型不完整,无法定义
TD e;
^
进度cc:15:50:错误:聚合“TD e2”的类型不完整,无法定义
TD-e2;
第17条:

prog.cc:14:24: error: aggregate 'TD<void() noexcept> e' has incomplete type and cannot be defined
 TD<decltype(no_throw)> e;
                        ^
prog.cc:15:50: error: aggregate 'TD<void (&)() noexcept> e2' has incomplete type and cannot be defined
 TD<decltype(std::declval<decltype(no_throw)>())> e2;
prog.cc:14:24:错误:聚合“TD e”的类型不完整,无法定义
TD e;
^
进度cc:15:50:错误:聚合“TD e2”的类型不完整,无法定义
TD-e2;

请看,在17中,noexcept现在是类型

您对编译器错误的看法是正确的,但它只是一个括号输入错误。我已经在我原来的帖子中修复了这个问题;问题是我不理解为什么断言在函数声明之后不添加()就通过了。如果它是一个重载集,您就不能使用
decltype(no_throw)
:/