[英]noexcept(false) Destructor overrides all special member functions' exception specification?
考慮這個T
類
struct T{
T() noexcept (true) {}
T(T&& ) noexcept (true) {}
T(const T& ) noexcept (true) {}
T& operator = (T&&) noexcept(true) { return *this; }
T& operator = (const T&) noexcept(true) { return *this; }
~T() noexcept(false) {}
};
考慮這個簡單的測試程序:
int main(){
constexpr bool default_ctor = noexcept(T());
static_assert(default_ctor == true, "Default Constructor can throw exceptions");
constexpr bool move_ctor = noexcept(T(std::declval<T>())); //Move ctor
static_assert(move_ctor == true, "Move Constructor can throw exceptions");
constexpr bool copy_ctor = noexcept(T(std::declval<T&>())); //Copy ctor
static_assert(copy_ctor == true, "Copy Constructor can throw exceptions");
constexpr bool move_assign = noexcept(std::declval<T>() = std::declval<T>());
static_assert(move_ctor == true, "Move Assignment can throw exceptions");
constexpr bool copy_assign = noexcept(std::declval<T&>() = std::declval<const T&>());
static_assert(copy_ctor == true, "Copy Assignment can throw exceptions");
//It doesn't matter when using *`type_traits`* either. Same behavior:
constexpr bool no_throw_cons = std::is_nothrow_constructible<T>::value;
static_assert(no_throw_cons == true, "Default Constructor isn't nothrow");
//...others skipped for brevity
}
這里每一個static_assert
都會觸發。 這不應該是我從標准中理解的:
但是,當你在沒有異常規范的情況下聲明T
的析構函數noexcept(true)
在這個簡單的上下文中與noexcept(true)
相同)時,所有的斷言都會傳遞!
但是,運行時遵循規范:
struct T{
T() noexcept (true) { throw int(8); }
//.... there rest omitted for brevity
~T() noexcept(false) {}
};
int main(){
T a;
(void)a;
};
按預期調用std::terminate
。
是否有C ++標准的任何部分定義或暗示這種行為? 析構函數上的noexcept (false)
符僅在編譯時覆蓋每個特殊成員函數的異常規范?
或者這是每個主要編譯器中的前端錯誤。
在您的第一個測試中,您詢問完整表達式T()
可以拋出異常。 該表達式構造一個T
然后再次銷毀它 。 因此,如果析構函數可以拋出,那么表達式也可以拋出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.