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

c – 如果count()是constexpr函数,为什么std :: array不能编译?

参见英文答案 > constexpr not working if the function is declared inside class scope3个
为什么以下C代码无法用VC2017编译?
struct FixedMatchResults
{
    static constexpr std::size_t count() { return 20; };

    std::array<int,count()> results;
};

错误是:

error C2975: ‘_Size’: invalid template argument for ‘std::array’,
expected compile-time constant expression

解决方法

在完整的结构定义之后解析函数体.这是为了允许您可能引用在函数体之后定义的其他成员.

但是,这意味着当编译器解析结果时,它没有count函数的主体,因此无法运行它.

有关更详细的答案,请参阅此问题:constexpr not working if the function is declared inside class scope

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

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

相关推荐