类本身不能具有静态constexpr成员实例吗? [英] Can't a class have static constexpr member instances of itself?

查看:125
本文介绍了类本身不能具有静态constexpr成员实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码给我类型不完整错误. 问题是什么?不允许类具有自身的静态成员实例吗? 有没有办法达到相同的结果?

This code is giving me incomplete type error. What is the problem? Isn't allowed for a class to have static member instances of itself? Is there a way to achieve the same result?

struct Size
{
    const unsigned int width;
    const unsigned int height;

    static constexpr Size big = { 480, 240 };

    static constexpr Size small = { 210, 170 };

private:

    Size( ) = default;
};

推荐答案

有没有办法达到相同的结果?

Is there a way to achieve the same result?

通过相同结果",您是否特别打算将 Size::bigSize::small?在那种情况下,这可能足够接近了:

By "the same result", do you specifically intend the constexpr-ness of Size::big and Size::small? In that case maybe this would be close enough:

struct Size
{
    const unsigned int width = 0;
    const unsigned int height = 0;

    static constexpr Size big() {
        return Size { 480, 240 };
    }

    static constexpr Size small() {
        return Size { 210, 170 };
    }

private:

    constexpr Size() = default;
    constexpr Size(int w, int h )
    : width(w),height(h){}
};

static_assert(Size::big().width == 480,"");
static_assert(Size::small().height == 170,"");

这篇关于类本身不能具有静态constexpr成员实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆