Skip to content

Commit

Permalink
[libc++] Complete overhaul of constexpr support in std::array
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ldionne committed May 28, 2020
1 parent b726d07 commit 77b9abf
Show file tree
Hide file tree
Showing 38 changed files with 1,159 additions and 788 deletions.
2 changes: 2 additions & 0 deletions libcxx/docs/FeatureTestMacroTable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ Status
------------------------------------------------- -----------------
**C++ 2a**
-------------------------------------------------------------------
``__cpp_lib_array_constexpr`` ``201811L``
------------------------------------------------- -----------------
``__cpp_lib_atomic_ref`` *unimplemented*
------------------------------------------------- -----------------
``__cpp_lib_bind_front`` *unimplemented*
Expand Down
156 changes: 89 additions & 67 deletions libcxx/include/array
Original file line number Diff line number Diff line change
Expand Up @@ -32,71 +32,76 @@ struct array
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
// No explicit construct/copy/destroy for aggregate type
void fill(const T& u);
void swap(array& a) noexcept(is_nothrow_swappable_v<T>);
void fill(const T& u); // constexpr in C++20
void swap(array& a) noexcept(is_nothrow_swappable_v<T>); // constexpr in C++20
// iterators:
iterator begin() noexcept;
const_iterator begin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
iterator begin() noexcept; // constexpr in C++17
const_iterator begin() const noexcept; // constexpr in C++17
iterator end() noexcept; // constexpr in C++17
const_iterator end() const noexcept; // constexpr in C++17
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
reverse_iterator rbegin() noexcept; // constexpr in C++17
const_reverse_iterator rbegin() const noexcept; // constexpr in C++17
reverse_iterator rend() noexcept; // constexpr in C++17
const_reverse_iterator rend() const noexcept; // constexpr in C++17
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
const_iterator cbegin() const noexcept; // constexpr in C++17
const_iterator cend() const noexcept; // constexpr in C++17
const_reverse_iterator crbegin() const noexcept; // constexpr in C++17
const_reverse_iterator crend() const noexcept; // constexpr in C++17
// capacity:
constexpr size_type size() const noexcept;
constexpr size_type max_size() const noexcept;
constexpr bool empty() const noexcept;
// element access:
reference operator[](size_type n);
const_reference operator[](size_type n) const; // constexpr in C++14
const_reference at(size_type n) const; // constexpr in C++14
reference at(size_type n);
reference front();
const_reference front() const; // constexpr in C++14
reference back();
const_reference back() const; // constexpr in C++14
T* data() noexcept;
const T* data() const noexcept;
reference operator[](size_type n); // constexpr in C++17
const_reference operator[](size_type n) const; // constexpr in C++14
reference at(size_type n); // constexpr in C++17
const_reference at(size_type n) const; // constexpr in C++14
reference front(); // constexpr in C++17
const_reference front() const; // constexpr in C++14
reference back(); // constexpr in C++17
const_reference back() const; // constexpr in C++14
T* data() noexcept; // constexpr in C++17
const T* data() const noexcept; // constexpr in C++17
};
template <class T, class... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>;
template <class T, class... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>; // C++17
template <class T, size_t N>
bool operator==(const array<T,N>& x, const array<T,N>& y);
bool operator==(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
template <class T, size_t N>
bool operator!=(const array<T,N>& x, const array<T,N>& y);
bool operator!=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
template <class T, size_t N>
bool operator<(const array<T,N>& x, const array<T,N>& y);
bool operator<(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
template <class T, size_t N>
bool operator>(const array<T,N>& x, const array<T,N>& y);
bool operator>(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
template <class T, size_t N>
bool operator<=(const array<T,N>& x, const array<T,N>& y);
bool operator<=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
template <class T, size_t N>
bool operator>=(const array<T,N>& x, const array<T,N>& y);
bool operator>=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
template <class T, size_t N >
void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // C++17
void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
template <class T, size_t N>
constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]); // C++20
template <class T, size_t N>
constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]); // C++20
template <class T> struct tuple_size;
template <size_t I, class T> struct tuple_element;
template <class T, size_t N> struct tuple_size<array<T, N>>;
template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
} // std
Expand Down Expand Up @@ -143,11 +148,12 @@ struct _LIBCPP_TEMPLATE_VIS array
_Tp __elems_[_Size];

// No explicit construct/copy/destroy for aggregate type
_LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) {
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void fill(const value_type& __u) {
_VSTD::fill_n(__elems_, _Size, __u);
}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);
}
Expand Down Expand Up @@ -236,50 +242,71 @@ struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

#ifndef _LIBCPP_CXX03_LANG
union __wrapper {
_LIBCPP_CONSTEXPR __wrapper() : __b() { }
~__wrapper() = default;

bool __b;
_Tp __t;
} __w;

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
value_type* data() _NOEXCEPT {return &__w.__t;}
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const value_type* data() const _NOEXCEPT {return &__w.__t;}
#else // C++03
typedef typename conditional<is_const<_Tp>::value, const char,
char>::type _CharType;

struct _ArrayInStructT { _Tp __data_[1]; };
_ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];

_LIBCPP_INLINE_VISIBILITY
value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);}
_LIBCPP_INLINE_VISIBILITY
const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);}
#endif

// No explicit construct/copy/destroy for aggregate type
_LIBCPP_INLINE_VISIBILITY void fill(const value_type&) {
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void fill(const value_type&) {
static_assert(!is_const<_Tp>::value,
"cannot fill zero-sized array of type 'const T'");
}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void swap(array&) _NOEXCEPT {
static_assert(!is_const<_Tp>::value,
"cannot swap zero-sized array of type 'const T'");
}

// iterators:
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
iterator begin() _NOEXCEPT {return iterator(data());}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
iterator end() _NOEXCEPT {return iterator(data());}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const_iterator end() const _NOEXCEPT {return const_iterator(data());}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const_iterator cbegin() const _NOEXCEPT {return begin();}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const_iterator cend() const _NOEXCEPT {return end();}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
const_reverse_iterator crend() const _NOEXCEPT {return rend();}

// capacity:
Expand All @@ -291,7 +318,7 @@ struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
_LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}

// element access:
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reference operator[](size_type) _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
_LIBCPP_UNREACHABLE();
Expand All @@ -303,46 +330,41 @@ struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
_LIBCPP_UNREACHABLE();
}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reference at(size_type) {
__throw_out_of_range("array<T, 0>::at");
_LIBCPP_UNREACHABLE();
}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const_reference at(size_type) const {
__throw_out_of_range("array<T, 0>::at");
_LIBCPP_UNREACHABLE();
}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reference front() _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
_LIBCPP_UNREACHABLE();
}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const_reference front() const _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
_LIBCPP_UNREACHABLE();
}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reference back() _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
_LIBCPP_UNREACHABLE();
}

_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const_reference back() const _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
_LIBCPP_UNREACHABLE();
}

_LIBCPP_INLINE_VISIBILITY
value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);}
_LIBCPP_INLINE_VISIBILITY
const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);}
};


Expand Down Expand Up @@ -404,7 +426,7 @@ operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
}

template <class _Tp, size_t _Size>
inline _LIBCPP_INLINE_VISIBILITY
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
typename enable_if
<
_Size == 0 ||
Expand Down
5 changes: 4 additions & 1 deletion libcxx/include/version
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ __cpp_lib_allocator_traits_is_always_equal 201411L <memory> <scoped
<unordered_map> <unordered_set>
__cpp_lib_any 201606L <any>
__cpp_lib_apply 201603L <tuple>
__cpp_lib_array_constexpr 201603L <iterator> <array>
__cpp_lib_array_constexpr 201811L <iterator> <array>
201603L // C++17
__cpp_lib_as_const 201510L <utility>
__cpp_lib_atomic_is_always_lock_free 201603L <atomic>
__cpp_lib_atomic_ref 201806L <atomic>
Expand Down Expand Up @@ -212,6 +213,8 @@ __cpp_lib_void_t 201411L <type_traits>
#endif

#if _LIBCPP_STD_VER > 17
# undef __cpp_lib_array_constexpr
# define __cpp_lib_array_constexpr 201811L
# if !defined(_LIBCPP_HAS_NO_THREADS)
// # define __cpp_lib_atomic_ref 201806L
# endif
Expand Down
50 changes: 50 additions & 0 deletions libcxx/test/std/containers/sequences/array/aggregate.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// Make sure std::array is an aggregate type.

#include <array>
#include <type_traits>

template <typename T>
void tests()
{
// Test aggregate initialization
{
std::array<T, 0> a0 = {}; (void)a0;
std::array<T, 1> a1 = {T()}; (void)a1;
std::array<T, 2> a2 = {T(), T()}; (void)a2;
std::array<T, 3> a3 = {T(), T(), T()}; (void)a3;
}

// Test the is_aggregate trait.
#if TEST_STD_VER >= 17 // The trait is only available in C++17 and above
static_assert(std::is_aggregate<std::array<T, 0> >::value, "");
static_assert(std::is_aggregate<std::array<T, 1> >::value, "");
static_assert(std::is_aggregate<std::array<T, 2> >::value, "");
static_assert(std::is_aggregate<std::array<T, 3> >::value, "");
static_assert(std::is_aggregate<std::array<T, 4> >::value, "");
#endif
}

struct Empty { };
struct NonEmpty { int i; int j; };

int main(int, char**)
{
tests<char>();
tests<int>();
tests<long>();
tests<float>();
tests<double>();
tests<long double>();
tests<NonEmpty>();
tests<Empty>();

return 0;
}
Loading

0 comments on commit 77b9abf

Please sign in to comment.