6

The following code doesn't compile with clang and 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;
}

But if you change the 0 to a 1, it compiles.

Is this a bug in libc++ or is there a good reason for it? How could I work around this if I have generic constexpr code that uses begin/end?

(I've seen this question, but my example intentionally uses static to avoid this problem.)

jtbandes
  • 115,675
  • 35
  • 233
  • 266

1 Answers1

5

I believe this is a bug in libc++ which was introduced in: https://reviews.llvm.org/D41223

In particular, the _LIBCPP_CONSTEXPR_AFTER_CXX14 is missing on begin(), end(), etc.

It seems to work in libstdc++: https://godbolt.org/z/xmfdot

The bug has been reported at https://bugs.llvm.org/show_bug.cgi?id=40124 but it sounds like it may not be fixed due to difficulties obtaining a constexpr pointer to an object when one has not been constructed.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 1
    +1. https://libcxx.llvm.org/ says: "If you think you've found a bug in libc++, please report it using the [LLVM Bugzilla](https://bugs.llvm.org/). If you're not sure, you can post a message to the [libcxx-dev](https://lists.llvm.org/mailman/listinfo/libcxx-dev) mailing list or on IRC." – ruakh Feb 29 '20 at 17:36