【问题标题】:How can I static_assert on types?我怎样才能对类型进行静态断言?
【发布时间】:2013-11-07 08:28:42
【问题描述】:

受到this question 上的一位 cmets 的启发,我想在我的代码中编写此代码,因为我可能做出了错误的假设,如果我将代码移植到两种类型不相同的平台上,则需要进行调查。

static_assert(typeid(float) == typeid(GLfloat), "GLfloat is unexpected type");

但是因为error: call to non-constexpr function ‘bool std::type_info::operator==(const std::type_info&) const’ 没有编译

但是我可以这样写:-

static_assert(sizeof(float) == sizeof(GLfloat), "GLfloat is unexpected size");

而且它按预期工作。 如果我的假设在新平台上是错误的,这很可能足以给我一个编译时错误,但我想知道是否有任何方法可以实现我真正想要的 - 比较实际类型?

【问题讨论】:

    标签: c++


    【解决方案1】:

    使用特征:

    #include <type_traits>
    
    static_assert(std::is_same<float, GLfloat>::value, "GLfloat is not float");
    

    【讨论】:

    • 啊,对了,我忘记了这些。记住这些就容易了。
    猜你喜欢
    • 2013-10-17
    • 1970-01-01
    • 2021-06-29
    • 2018-03-26
    • 2015-10-30
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多