c++ - 如果 count() 是 constexpr 函数,为什么 std::array<int, count()> 不能编译?

标签 c++ visual-c++

这个问题在这里已经有了答案:





constexpr not working if the function is declared inside class scope

(3 个回答)


3年前关闭。




为什么下面的 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

最佳答案

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

然而这意味着当编译器解析 results 时它没有 count 的主体函数,所以它不能运行它。

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

关于c++ - 如果 count() 是 constexpr 函数,为什么 std::array<int, count()> 不能编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52077397/

相关文章:

c++ - Mac OSX 上 Qt App 中完全随机的 malloc 错误

c++ - 在字符串的情况下,在 C++ 中重载 = 运算符

C++变量命名

c - C 中使用 %2 将十进制转换为二进制

c++ - 如何调用作为槽的qt函数?

C++,STL,映射如何用值而不是键排序

c++ - 表达式必须有指针类型错误 visual studio 2012

c++ - 是否有以下C++阻塞条件变量

c++ - std::set 使用 char * 类型查找行为

c++ - 为什么我可以在 MSVC 和 icc 中为 glm::vec 创建用户定义的结构化绑定(bind),但不能在 Clang 和 GCC 中创建?