[libc++] Complete overhaul of constexpr support in std::array
This commit adds missing support for constexpr in std::array under all standard modes up to and including C++20. It also transforms the <array> tests to check for constexpr-friendliness under the right standard modes. Fixes https://llvm.org/PR40124 Fixes rdar://57522096 Supersedes https://reviews.llvm.org/D60666 Differential Revision: https://reviews.llvm.org/D80452
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
#include "disable_missing_braces_warning.h"
|
||||
|
||||
struct NoDefault {
|
||||
NoDefault(int) {}
|
||||
TEST_CONSTEXPR NoDefault(int) { }
|
||||
};
|
||||
|
||||
#if TEST_STD_VER < 11
|
||||
@@ -33,7 +33,7 @@ struct natural_alignment {
|
||||
};
|
||||
#endif
|
||||
|
||||
int main(int, char**)
|
||||
TEST_CONSTEXPR_CXX17 bool tests()
|
||||
{
|
||||
{
|
||||
typedef double T;
|
||||
@@ -52,33 +52,49 @@ int main(int, char**)
|
||||
LIBCPP_ASSERT(p != nullptr);
|
||||
}
|
||||
{
|
||||
typedef double T;
|
||||
typedef std::array<const T, 0> C;
|
||||
C c = {{}};
|
||||
const T* p = c.data();
|
||||
static_assert((std::is_same<decltype(c.data()), const T*>::value), "");
|
||||
LIBCPP_ASSERT(p != nullptr);
|
||||
}
|
||||
{
|
||||
#if TEST_STD_VER < 11
|
||||
typedef natural_alignment T;
|
||||
#else
|
||||
typedef std::max_align_t T;
|
||||
#endif
|
||||
typedef std::array<T, 0> C;
|
||||
const C c = {};
|
||||
const T* p = c.data();
|
||||
LIBCPP_ASSERT(p != nullptr);
|
||||
std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
|
||||
assert(pint % TEST_ALIGNOF(T) == 0);
|
||||
typedef double T;
|
||||
typedef std::array<const T, 0> C;
|
||||
C c = {{}};
|
||||
const T* p = c.data();
|
||||
LIBCPP_ASSERT(p != nullptr);
|
||||
static_assert((std::is_same<decltype(c.data()), const T*>::value), "");
|
||||
}
|
||||
{
|
||||
typedef NoDefault T;
|
||||
typedef std::array<T, 0> C;
|
||||
C c = {};
|
||||
T* p = c.data();
|
||||
LIBCPP_ASSERT(p != nullptr);
|
||||
typedef NoDefault T;
|
||||
typedef std::array<T, 0> C;
|
||||
C c = {};
|
||||
T* p = c.data();
|
||||
LIBCPP_ASSERT(p != nullptr);
|
||||
}
|
||||
{
|
||||
std::array<int, 5> c = {0, 1, 2, 3, 4};
|
||||
assert(c.data() == &c[0]);
|
||||
assert(*c.data() == c[0]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
tests();
|
||||
#if TEST_STD_VER >= 17
|
||||
static_assert(tests(), "");
|
||||
#endif
|
||||
|
||||
// Test the alignment of data()
|
||||
{
|
||||
#if TEST_STD_VER < 11
|
||||
typedef natural_alignment T;
|
||||
#else
|
||||
typedef std::max_align_t T;
|
||||
#endif
|
||||
typedef std::array<T, 0> C;
|
||||
const C c = {};
|
||||
const T* p = c.data();
|
||||
LIBCPP_ASSERT(p != nullptr);
|
||||
std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
|
||||
assert(pint % TEST_ALIGNOF(T) == 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user