繁体   English   中英

空 std::array<T, 0> 没有 constexpr begin() 吗?

[英]Empty std::array<T, 0> doesn't have constexpr begin()?

以下代码不能用 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;
}

但是,如果您将0更改为1 ,它会编译。

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM