如何static_assert解析为 const 的值是正确的类型?

How do I static_assert that a value parsed as const& is the right type?

本文关键字:类型 const static assert 如何      更新时间:2023-10-16
MyClass::MyClass(std::list<int> const& some_sequence)
    {
    static_assert(
        std::is_same<decltype(some_sequence),std::list<int>>::value ,
        "some_sequence should be an integer list"
        );
    }

如何使静态断言工作?重要的是类型是一个整数列表。干杯。

不需要使用

static_assert(...) :编译器将确保使用std::list<int>调用此函数。如果要使上面的代码编译,则需要使用

MyClass::MyClass(std::list<int> const& some_sequence)
{
    static_assert(
        std::is_same<decltype(some_sequence),std::list<int> const&>::value ,
        "some_sequence should be an integer list"
    );
}

some_sequence被声明为std::list<int> const&,这就是由 decltype(some_sequence) 获得的类型。然而,这种static_assert()永远不会失败。

相关文章: