[問題] noexcept 用法

看板C_and_CPP (C/C++)作者 (王大明)時間4年前 (2021/06/21 22:27), 編輯推噓0(007)
留言7則, 1人參與, 4年前最新討論串1/1
最近看到一段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

06/21 23:37, 4年前 , 1F
嗯... new expression 本身就非 noexcept
06/21 23:37, 1F

06/21 23:38, 4年前 , 2F
建議可去看一下cppreference的 noexcept specifier頁面
06/21 23:38, 2F

06/21 23:40, 4年前 , 3F
阿抱歉 突然發現你的code是用 placement new
06/21 23:40, 3F

06/21 23:42, 4年前 , 4F
你可以在reference裡看noexcept規則
06/21 23:42, 4F

06/21 23:42, 4年前 , 5F
基本上沒有標noexcept的func就是potentially throwing
06/21 23:42, 5F

06/21 23:43, 4年前 , 6F
但有例外 像是符合條件的implicit constructor 規則有
06/21 23:43, 6F

06/21 23:43, 4年前 , 7F
點複雜
06/21 23:43, 7F
文章代碼(AID): #1WqA6zq6 (C_and_CPP)
文章代碼(AID): #1WqA6zq6 (C_and_CPP)