微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 内部类的奇怪的constexpr行为

有人可以试试解释一下吗?
template<typename T,size_t S = T::noElems()>
struct C
{
};

struct X
{
    enum E { A,B,C };
    static constexpr size_t noElems() { return C+1; };
};

struct K
{
    C<X> cx; // this DOES compile
};

struct Y
{
    struct Z
    {
        enum E { A,C };
        static constexpr size_t noElems() { return C+1; };
    };
    C<Z,Z::C+1> cyz; // this DOES compile

    C<Z> cyz; // <--- this does NOT compile 
};

解决方法

用struct的声明
struct Y
{
    struct Z
    {
        enum E { A,Z::C+1> cyz1; // this DOES compile

    C<Z> cyz2; // <--- this does NOT compile 
};

实体cyz1和cyz2在Z :: noElems()的内联声明之前解析,所以定义

static constexpr size_t noElems() { return C+1; };

在宣布时不可用

C<Z> cyz2;

原文地址:https://www.jb51.cc/c/114109.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐