作者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://www.ptt.cc/bbs/C_and_CPP/M.1624285629.A.D06.html
→ g0010726: 嗯... new expression 本身就非 noexcept 06/21 23:37
→ g0010726: 建議可去看一下cppreference的 noexcept specifier頁面 06/21 23:38
→ g0010726: 阿抱歉 突然發現你的code是用 placement new 06/21 23:40
→ g0010726: 你可以在reference裡看noexcept規則 06/21 23:42
→ g0010726: 基本上沒有標noexcept的func就是potentially throwing 06/21 23:42
→ g0010726: 但有例外 像是符合條件的implicit constructor 規則有 06/21 23:43
→ g0010726: 點複雜 06/21 23:43