Noexcept (false) Does the destructor override the exception specification of all special members?

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>())); //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
}

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); }
    //.... there rest omitted for brevity
    ~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 .

+4
1

, T() . a T, . , , .

+2

Source: https://habr.com/ru/post/1653363/


All Articles