Consider this class 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) {}
};
Consider this simple test program:
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>()));
static_assert(move_ctor == true, "Move Constructor can throw exceptions");
constexpr bool copy_ctor = noexcept(T(std::declval<T&>()));
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");
constexpr bool no_throw_cons = std::is_nothrow_constructible<T>::value;
static_assert(no_throw_cons == true, "Default Constructor isn't nothrow");
}
Here everyone starts static_assert. This should not be consistent with what I understand from the standard:
But, when you declare a destructor Twithout an exception specification (the same as noexcept(true)in this simple context), all statements are passed!
However, the runtime complies with the specification:
struct T{
T() noexcept (true) { throw int(8); }
~T() noexcept(false) {}
};
int main(){
T a;
(void)a;
};
std::terminate called as expected.
Is there any part of the C ++ standard that defines or implies this behavior? What does the specifier noexcept (false)for the destructor override the exception specification for each special member function only at compile time?
Front-End .