作者WangDaMing (王大明)
看板C_and_CPP
标题[问题] noexcept 用法
时间Mon Jun 21 22:27:07 2021
最近看到一段code,我猜作用应该是检查是否有noexcept属性,所以用自己的方式写了一下
请问这样的确可以在compile time检查这个class嘛??
其实我也不是很懂原理,这个new是让compiler会启动class 检查嘛??
不确定这样做没问题嘛?
#include <iostream>
class TEST{
public:
TEST(){
throw 100;
}
};
int main()
{
if(noexcept(new (static_cast<TEST*>(nullptr)) TEST()))
{
printf("noexcept\n");
}else{
printf("not noexcept\n");
}
}
感谢!!
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.230.115.32 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1624285629.A.D06.html
1F:→ g0010726: 嗯... new expression 本身就非 noexcept 06/21 23:37
2F:→ g0010726: 建议可去看一下cppreference的 noexcept specifier页面 06/21 23:38
3F:→ g0010726: 阿抱歉 突然发现你的code是用 placement new 06/21 23:40
4F:→ g0010726: 你可以在reference里看noexcept规则 06/21 23:42
5F:→ g0010726: 基本上没有标noexcept的func就是potentially throwing 06/21 23:42
6F:→ g0010726: 但有例外 像是符合条件的implicit constructor 规则有 06/21 23:43
7F:→ g0010726: 点复杂 06/21 23:43