gpt4 book ai didi

c++ - 空 std::array 没有 constexpr begin()?

转载 作者:行者123 更新时间:2023-12-01 13:03:57 25 4
gpt4 key购买 nike

以下代码不能用 clang 和 libc++ 编译:https://godbolt.org/z/rDL-_K

#include <array>
int main() {
static constexpr std::array<int, 0> xs{};
constexpr auto i = xs.begin(); // non-constexpr function 'begin' cannot be used in a constant expression

return 0;
}

但是如果你改变了 01 ,它编译。

这是 libc++ 中的错误还是有充分的理由?如果我有使用 begin 的通用 constexpr 代码,我该如何解决这个问题?/ end ?

(我见过 this question ,但我的示例故意使用 static 来避免这个问题。)

最佳答案

我相信这是在 libc++ 中引入的一个错误:https://reviews.llvm.org/D41223

特别是_LIBCPP_CONSTEXPR_AFTER_CXX14缺少 begin()、end() 等。

它似乎在 libstdc++ 中工作:https://godbolt.org/z/xmfdot

该错误已在 https://bugs.llvm.org/show_bug.cgi?id=40124 报告但听起来它可能无法修复,因为在尚未构造对象时难以获得指向对象的 constexpr 指针。

关于c++ - 空 std::array<T, 0> 没有 constexpr begin()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60462569/

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