Make std::get constexpr

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@186525 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2013-07-17 18:25:36 +00:00
parent f7777e8554
commit 8fc4f5a251
12 changed files with 299 additions and 51 deletions

View File

@ -79,47 +79,47 @@ template <class _T1, class _T2> struct __tuple_like<pair<_T1, _T2> > : true_type
template <class _Tp, size_t _Size> struct __tuple_like<array<_Tp, _Size> > : true_type {};
template <size_t _Ip, class ..._Tp>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Ip, tuple<_Tp...> >::type&
get(tuple<_Tp...>&) _NOEXCEPT;
template <size_t _Ip, class ..._Tp>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const typename tuple_element<_Ip, tuple<_Tp...> >::type&
get(const tuple<_Tp...>&) _NOEXCEPT;
template <size_t _Ip, class ..._Tp>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Ip, tuple<_Tp...> >::type&&
get(tuple<_Tp...>&&) _NOEXCEPT;
template <size_t _Ip, class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Ip, pair<_T1, _T2> >::type&
get(pair<_T1, _T2>&) _NOEXCEPT;
template <size_t _Ip, class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
get(const pair<_T1, _T2>&) _NOEXCEPT;
template <size_t _Ip, class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
get(pair<_T1, _T2>&&) _NOEXCEPT;
template <size_t _Ip, class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp&
get(array<_Tp, _Size>&) _NOEXCEPT;
template <size_t _Ip, class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const _Tp&
get(const array<_Tp, _Size>&) _NOEXCEPT;
template <size_t _Ip, class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp&&
get(array<_Tp, _Size>&&) _NOEXCEPT;

View File

@ -59,14 +59,14 @@ struct array
// element access:
reference operator[](size_type n);
const_reference operator[](size_type n) const;
const_reference at(size_type n) const;
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;
const_reference front() const; // constexpr in C++14
reference back();
const_reference back() const;
const_reference back() const; // constexpr in C++14
T* data() noexcept;
const T* data() const noexcept;
@ -92,9 +92,9 @@ template <class T> class tuple_size;
template <int I, class T> class tuple_element;
template <class T, size_t N> struct tuple_size<array<T, N>>;
template <int I, class T, size_t N> struct tuple_element<I, array<T, N>>;
template <int I, class T, size_t N> T& get(array<T, N>&) noexcept;
template <int I, class T, size_t N> const T& get(const array<T, N>&) noexcept;
template <int I, class T, size_t N> T&& get(array<T, N>&&) noexcept;
template <int I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
template <int I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
template <int I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
} // std
@ -181,14 +181,14 @@ struct _LIBCPP_TYPE_VIS array
// element access:
_LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __elems_[__n];}
_LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __elems_[__n];}
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference operator[](size_type __n) const {return __elems_[__n];}
reference at(size_type __n);
const_reference at(size_type __n) const;
_LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
_LIBCPP_INLINE_VISIBILITY reference front() {return __elems_[0];}
_LIBCPP_INLINE_VISIBILITY const_reference front() const {return __elems_[0];}
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];}
_LIBCPP_INLINE_VISIBILITY reference back() {return __elems_[_Size > 0 ? _Size-1 : 0];}
_LIBCPP_INLINE_VISIBILITY const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];}
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];}
_LIBCPP_INLINE_VISIBILITY
value_type* data() _NOEXCEPT {return __elems_;}
@ -210,6 +210,7 @@ array<_Tp, _Size>::at(size_type __n)
}
template <class _Tp, size_t _Size>
_LIBCPP_CONSTEXPR_AFTER_CXX11
typename array<_Tp, _Size>::const_reference
array<_Tp, _Size>::at(size_type __n) const
{
@ -306,32 +307,32 @@ public:
};
template <size_t _Ip, class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
_LIBCPP_INLINE_VISIBILITY inline _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp&
get(array<_Tp, _Size>& __a) _NOEXCEPT
{
static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
return __a[_Ip];
return __a.__elems_[_Ip];
}
template <size_t _Ip, class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
_LIBCPP_INLINE_VISIBILITY inline _LIBCPP_CONSTEXPR_AFTER_CXX11
const _Tp&
get(const array<_Tp, _Size>& __a) _NOEXCEPT
{
static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
return __a[_Ip];
return __a.__elems_[_Ip];
}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
template <size_t _Ip, class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
_LIBCPP_INLINE_VISIBILITY inline _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp&&
get(array<_Tp, _Size>&& __a) _NOEXCEPT
{
static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
return _VSTD::move(__a[_Ip]);
return _VSTD::move(__a.__elems_[_Ip]);
}
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES

View File

@ -86,13 +86,13 @@ template <intsize_t I, class... T> class tuple_element<I, tuple<T...>>;
// 20.4.1.5, element access:
template <intsize_t I, class... T>
typename tuple_element<I, tuple<T...>>::type&
get(tuple<T...>&) noexcept;
get(tuple<T...>&) noexcept; // constexpr in C++14
template <intsize_t I, class... T>
typename tuple_element<I, tuple<T...>>::type const&
get(const tuple<T...>&) noexcept;
get(const tuple<T...>&) noexcept; // constexpr in C++14
template <intsize_t I, class... T>
typename tuple_element<I, tuple<T...>>::type&&
get(tuple<T...>&&) noexcept;
get(tuple<T...>&&) noexcept; // constexpr in C++14
template <class T1, class... T>
constexpr T1& get(tuple<T...>&) noexcept; // C++14
@ -546,11 +546,11 @@ class _LIBCPP_TYPE_VIS tuple
base base_;
template <size_t _Jp, class ..._Up> friend
template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Jp, tuple<_Up...> >::type& get(tuple<_Up...>&) _NOEXCEPT;
template <size_t _Jp, class ..._Up> friend
template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
const typename tuple_element<_Jp, tuple<_Up...> >::type& get(const tuple<_Up...>&) _NOEXCEPT;
template <size_t _Jp, class ..._Up> friend
template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Jp, tuple<_Up...> >::type&& get(tuple<_Up...>&&) _NOEXCEPT;
public:
@ -763,7 +763,7 @@ swap(tuple<_Tp...>& __t, tuple<_Tp...>& __u)
// get
template <size_t _Ip, class ..._Tp>
inline _LIBCPP_INLINE_VISIBILITY
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Ip, tuple<_Tp...> >::type&
get(tuple<_Tp...>& __t) _NOEXCEPT
{
@ -772,7 +772,7 @@ get(tuple<_Tp...>& __t) _NOEXCEPT
}
template <size_t _Ip, class ..._Tp>
inline _LIBCPP_INLINE_VISIBILITY
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const typename tuple_element<_Ip, tuple<_Tp...> >::type&
get(const tuple<_Tp...>& __t) _NOEXCEPT
{
@ -781,7 +781,7 @@ get(const tuple<_Tp...>& __t) _NOEXCEPT
}
template <size_t _Ip, class ..._Tp>
inline _LIBCPP_INLINE_VISIBILITY
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Ip, tuple<_Tp...> >::type&&
get(tuple<_Tp...>&& __t) _NOEXCEPT
{

View File

@ -107,15 +107,15 @@ template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >;
template<size_t I, class T1, class T2>
typename tuple_element<I, std::pair<T1, T2> >::type&
get(std::pair<T1, T2>&) noexcept;
get(std::pair<T1, T2>&) noexcept; // constexpr in C++14
template<size_t I, class T1, class T2>
const typename const tuple_element<I, std::pair<T1, T2> >::type&
get(const std::pair<T1, T2>&) noexcept;
get(const std::pair<T1, T2>&) noexcept; // constexpr in C++14
template<size_t I, class T1, class T2>
typename tuple_element<I, std::pair<T1, T2> >::type&&
get(std::pair<T1, T2>&&) noexcept;
get(std::pair<T1, T2>&&) noexcept; // constexpr in C++14
template<class T1, class T2>
constexpr T1& get(std::pair<T1, T2>&) noexcept; // C++14
@ -546,13 +546,13 @@ struct __get_pair<0>
{
template <class _T1, class _T2>
static
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_T1&
get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
template <class _T1, class _T2>
static
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const _T1&
get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
@ -560,7 +560,7 @@ struct __get_pair<0>
template <class _T1, class _T2>
static
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_T1&&
get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
@ -572,13 +572,13 @@ struct __get_pair<1>
{
template <class _T1, class _T2>
static
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_T2&
get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
template <class _T1, class _T2>
static
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const _T2&
get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
@ -586,7 +586,7 @@ struct __get_pair<1>
template <class _T1, class _T2>
static
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_T2&&
get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
@ -594,7 +594,7 @@ struct __get_pair<1>
};
template <size_t _Ip, class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY inline
_LIBCPP_INLINE_VISIBILITY inline _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Ip, pair<_T1, _T2> >::type&
get(pair<_T1, _T2>& __p) _NOEXCEPT
{
@ -602,7 +602,7 @@ get(pair<_T1, _T2>& __p) _NOEXCEPT
}
template <size_t _Ip, class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY inline
_LIBCPP_INLINE_VISIBILITY inline _LIBCPP_CONSTEXPR_AFTER_CXX11
const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
get(const pair<_T1, _T2>& __p) _NOEXCEPT
{
@ -612,7 +612,7 @@ get(const pair<_T1, _T2>& __p) _NOEXCEPT
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
template <size_t _Ip, class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY inline
_LIBCPP_INLINE_VISIBILITY inline _LIBCPP_CONSTEXPR_AFTER_CXX11
typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
get(pair<_T1, _T2>&& __p) _NOEXCEPT
{

View File

@ -21,12 +21,16 @@ int main()
typedef std::array<T, 3> C;
C c = {1, 2, 3.5};
assert(c.size() == 3);
assert(c.max_size() == 3);
assert(!c.empty());
}
{
typedef double T;
typedef std::array<T, 0> C;
C c = {};
assert(c.size() == 0);
assert(c.max_size() == 0);
assert(c.empty());
}
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
{
@ -34,12 +38,16 @@ int main()
typedef std::array<T, 3> C;
constexpr C c = {1, 2, 3.5};
static_assert(c.size() == 3, "");
static_assert(c.max_size() == 3, "");
static_assert(!c.empty(), "");
}
{
typedef double T;
typedef std::array<T, 0> C;
constexpr C c = {};
static_assert(c.size() == 0, "");
static_assert(c.max_size() == 0, "");
static_assert(c.empty(), "");
}
#endif
}

View File

@ -14,6 +14,16 @@
#include <array>
#include <cassert>
#if __cplusplus > 201103L
struct S {
std::array<int, 3> a;
int k;
constexpr S() : a{1,2,3}, k(std::get<2>(a)) {}
};
constexpr std::array<int, 2> getArr () { return { 3, 4 }; }
#endif
int main()
{
{
@ -25,4 +35,18 @@ int main()
assert(c[1] == 5.5);
assert(c[2] == 3.5);
}
#if _LIBCPP_STD_VER > 11
{
typedef double T;
typedef std::array<T, 3> C;
constexpr C c = {1, 2, 3.5};
static_assert(std::get<0>(c) == 1, "");
static_assert(std::get<1>(c) == 2, "");
static_assert(std::get<2>(c) == 3.5, "");
}
{
static_assert(S().k == 3, "");
static_assert(std::get<1>(getArr()) == 4, "");
}
#endif
}

View File

@ -24,4 +24,14 @@ int main()
assert(std::get<1>(c) == 2);
assert(std::get<2>(c) == 3.5);
}
#if _LIBCPP_STD_VER > 11
{
typedef double T;
typedef std::array<T, 3> C;
constexpr const C c = {1, 2, 3.5};
static_assert(std::get<0>(c) == 1, "");
static_assert(std::get<1>(c) == 2, "");
static_assert(std::get<2>(c) == 3.5, "");
}
#endif
}

View File

@ -0,0 +1,67 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <array>
// reference operator[] (size_type)
// const_reference operator[] (size_type); // constexpr in C++14
// reference at (size_type)
// const_reference at (size_type); // constexpr in C++14
#include <array>
#include <cassert>
int main()
{
{
typedef double T;
typedef std::array<T, 3> C;
C c = {1, 2, 3.5};
C::reference r1 = c.at(0);
assert(r1 == 1);
r1 = 5.5;
assert(c.front() == 5.5);
C::reference r2 = c.at(2);
assert(r2 == 3.5);
r2 = 7.5;
assert(c.back() == 7.5);
try { (void) c.at(3); }
catch (const std::out_of_range &) {}
}
{
typedef double T;
typedef std::array<T, 3> C;
const C c = {1, 2, 3.5};
C::const_reference r1 = c.at(0);
assert(r1 == 1);
C::const_reference r2 = c.at(2);
assert(r2 == 3.5);
try { (void) c.at(3); }
catch (const std::out_of_range &) {}
}
#if _LIBCPP_STD_VER > 11
{
typedef double T;
typedef std::array<T, 3> C;
constexpr C c = {1, 2, 3.5};
constexpr T t1 = c.at(0);
static_assert (t1 == 1, "");
constexpr T t2 = c.at(2);
static_assert (t2 == 3.5, "");
}
#endif
}

View File

@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <array>
// reference front();
// reference back();
// const_reference front(); // constexpr in C++14
// const_reference back(); // constexpr in C++14
#include <array>
#include <cassert>
int main()
{
{
typedef double T;
typedef std::array<T, 3> C;
C c = {1, 2, 3.5};
C::reference r1 = c.front();
assert(r1 == 1);
r1 = 5.5;
assert(c[0] == 5.5);
C::reference r2 = c.back();
assert(r2 == 3.5);
r2 = 7.5;
assert(c[2] == 7.5);
}
{
typedef double T;
typedef std::array<T, 3> C;
const C c = {1, 2, 3.5};
C::const_reference r1 = c.front();
assert(r1 == 1);
C::const_reference r2 = c.back();
assert(r2 == 3.5);
}
#if _LIBCPP_STD_VER > 11
{
typedef double T;
typedef std::array<T, 3> C;
constexpr C c = {1, 2, 3.5};
constexpr T t1 = c.front();
static_assert (t1 == 1, "");
constexpr T t2 = c.back();
static_assert (t2 == 3.5, "");
}
#endif
}

View File

@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <array>
// reference operator[] (size_type)
// const_reference operator[] (size_type); // constexpr in C++14
// reference at (size_type)
// const_reference at (size_type); // constexpr in C++14
#include <array>
#include <cassert>
int main()
{
{
typedef double T;
typedef std::array<T, 3> C;
C c = {1, 2, 3.5};
C::reference r1 = c[0];
assert(r1 == 1);
r1 = 5.5;
assert(c.front() == 5.5);
C::reference r2 = c[2];
assert(r2 == 3.5);
r2 = 7.5;
assert(c.back() == 7.5);
}
{
typedef double T;
typedef std::array<T, 3> C;
const C c = {1, 2, 3.5};
C::const_reference r1 = c[0];
assert(r1 == 1);
C::const_reference r2 = c[2];
assert(r2 == 3.5);
}
#if _LIBCPP_STD_VER > 11
{
typedef double T;
typedef std::array<T, 3> C;
constexpr C c = {1, 2, 3.5};
constexpr T t1 = c[0];
static_assert (t1 == 1, "");
constexpr T t2 = c[2];
static_assert (t2 == 3.5, "");
}
#endif
}

View File

@ -31,10 +31,8 @@ int main()
{
typedef std::pair<int, short> P;
constexpr P p1(3, 4);
static_assert(p1.first == 3, "" ); // for now!
static_assert(p1.second == 4, "" ); // for now!
// static_assert(std::get<0>(p1) == 3, "");
// static_assert(std::get<1>(p1) == 4, "");
static_assert(std::get<0>(p1) == 3, "");
static_assert(std::get<1>(p1) == 4, "");
}
#endif
}

View File

@ -18,6 +18,16 @@
#include <utility>
#include <cassert>
#if __cplusplus > 201103L
struct S {
std::pair<int, int> a;
int k;
constexpr S() : a{1,2}, k(std::get<0>(a)) {}
};
constexpr std::pair<int, int> getP () { return { 3, 4 }; }
#endif
int main()
{
{
@ -30,4 +40,12 @@ int main()
assert(std::get<0>(p) == 5);
assert(std::get<1>(p) == 6);
}
#if __cplusplus > 201103L
{
static_assert(S().k == 1, "");
static_assert(std::get<1>(getP()) == 4, "");
}
#endif
}