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

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...