static assertion on noexcept for protected constructors

207 Views Asked by At

I'm attempting to assert following protected class constructors may or may not throw as follows:

#include <utility>
#include <type_traits>

class X
{
protected:
    X() noexcept(false) { }
    X(int) noexcept { }
};

int main()
{
    // should fail
    static_assert(noexcept(std::declval<X>().X()));

    // should pass
    static_assert(noexcept(std::declval<X>().X(3)));

    // will fail regardless of noexcept because constructor is protected,
    // but should pass if default ctor is noexcept and public
    static_assert(std::is_nothrow_constructible_v<X>);

    return 0;
}

static assertion should fail in first case and pass in second.

error is type name is not allowed and I'm not sure what syntax I should use or what would be better approach.

In third static_assert case it will always fail because constructor is not public.

0

There are 0 best solutions below