我试图在nothrow
的析构函数不抛出时指定函数为Foo
。我可以使用类型特征std::is_nothrow_destructible<>
来完成此操作。我怎么能直接这样做?我已经尝试了以下内容,但如果我取消注释注释行
#include <iostream>
#include <type_traits>
class Foo
{
public:
~Foo() noexcept {}
};
// void f() noexcept(noexcept(~Foo{})) { } // error here
void g() noexcept(std::is_nothrow_destructible<Foo>::value)
{
}
int main()
{
g();
}
我收到错误
error: no match for 'operator~' (operand type is 'Foo')
错误说明符noexcept(noexcept(~Foo()))
不正常,但对于构造函数,我可以使用noexcept(noexcept(Foo()))
。我在这里错过了一些明显的语法吗?
答案 0 :(得分:5)
只能通过成员访问表达式调用析构函数。所以语法是:
void f() noexcept(noexcept(std::declval<Foo>().~Foo()))