2

我试图断言以下受保护的类构造函数可能会或可能不会抛出如下:

#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;
}

静态断言应该在第一种情况下失败并在第二种情况下通过。

错误是type name is not allowed,我不确定我应该使用什么语法或更好的方法。

在第三种static_assert情况下,它总是会失败,因为构造函数不是公共的。

4

0 回答 0