gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:38:54 27 4
gpt4 key购买 nike

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





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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com