Bug 16599 part 2: Make std::pair's constructors and comparison operators (and make_pair) constexpr.
llvm-svn: 186430
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 18191ceb54c4e9a6e1cfc06f442f9eac87ae9472
diff --git a/include/utility b/include/utility
index 46a8803..2d2670d 100644
--- a/include/utility
+++ b/include/utility
@@ -66,10 +66,10 @@
pair(const pair&) = default;
pair(pair&&) = default;
constexpr pair();
- pair(const T1& x, const T2& y);
- template <class U, class V> pair(U&& x, V&& y);
- template <class U, class V> pair(const pair<U, V>& p);
- template <class U, class V> pair(pair<U, V>&& p);
+ pair(const T1& x, const T2& y); // constexpr in C++14
+ template <class U, class V> pair(U&& x, V&& y); // constexpr in C++14
+ template <class U, class V> pair(const pair<U, V>& p); // constexpr in C++14
+ template <class U, class V> pair(pair<U, V>&& p); // constexpr in C++14
template <class... Args1, class... Args2>
pair(piecewise_construct_t, tuple<Args1...> first_args,
tuple<Args2...> second_args);
@@ -83,14 +83,14 @@
noexcept(swap(second, p.second)));
};
-template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&);
-template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&);
-template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);
-template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&);
-template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&);
-template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&);
+template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
+template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
+template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
+template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
+template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
+template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
-template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&);
+template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
template <class T1, class T2>
void
swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
@@ -258,11 +258,12 @@
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR pair() : first(), second() {}
- _LIBCPP_INLINE_VISIBILITY pair(const _T1& __x, const _T2& __y)
+ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
+ pair(const _T1& __x, const _T2& __y)
: first(__x), second(__y) {}
template<class _U1, class _U2>
- _LIBCPP_INLINE_VISIBILITY
+ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair(const pair<_U1, _U2>& __p
#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
,typename enable_if<is_convertible<const _U1&, _T1>::value &&
@@ -271,6 +272,10 @@
)
: first(__p.first), second(__p.second) {}
+#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
+ _LIBCPP_INLINE_VISIBILITY
+ pair(const pair& __p) = default;
+#else
_LIBCPP_INLINE_VISIBILITY
pair(const pair& __p)
_NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
@@ -279,6 +284,7 @@
second(__p.second)
{
}
+#endif
_LIBCPP_INLINE_VISIBILITY
pair& operator=(const pair& __p)
@@ -295,20 +301,24 @@
template <class _U1, class _U2,
class = typename enable_if<is_convertible<_U1, first_type>::value &&
is_convertible<_U2, second_type>::value>::type>
- _LIBCPP_INLINE_VISIBILITY
+ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair(_U1&& __u1, _U2&& __u2)
: first(_VSTD::forward<_U1>(__u1)),
second(_VSTD::forward<_U2>(__u2))
{}
template<class _U1, class _U2>
- _LIBCPP_INLINE_VISIBILITY
+ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair(pair<_U1, _U2>&& __p,
typename enable_if<is_convertible<_U1, _T1>::value &&
is_convertible<_U2, _T2>::value>::type* = 0)
: first(_VSTD::forward<_U1>(__p.first)),
second(_VSTD::forward<_U2>(__p.second)) {}
+#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
+ _LIBCPP_INLINE_VISIBILITY
+ pair(pair&& __p) = default;
+#else
_LIBCPP_INLINE_VISIBILITY
pair(pair&& __p) _NOEXCEPT_(is_nothrow_move_constructible<first_type>::value &&
is_nothrow_move_constructible<second_type>::value)
@@ -316,6 +326,7 @@
second(_VSTD::forward<second_type>(__p.second))
{
}
+#endif
_LIBCPP_INLINE_VISIBILITY
pair&
@@ -331,7 +342,7 @@
template<class _Tuple,
class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
- _LIBCPP_INLINE_VISIBILITY
+ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair(_Tuple&& __p)
: first(_VSTD::forward<typename tuple_element<0,
typename __make_tuple_types<_Tuple>::type>::type>(get<0>(__p))),
@@ -387,7 +398,7 @@
};
template <class _T1, class _T2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool
operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
{
@@ -395,7 +406,7 @@
}
template <class _T1, class _T2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool
operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
{
@@ -403,7 +414,7 @@
}
template <class _T1, class _T2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
{
@@ -411,7 +422,7 @@
}
template <class _T1, class _T2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool
operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
{
@@ -419,7 +430,7 @@
}
template <class _T1, class _T2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool
operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
{
@@ -427,7 +438,7 @@
}
template <class _T1, class _T2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool
operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
{
@@ -472,7 +483,7 @@
};
template <class _T1, class _T2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
make_pair(_T1&& __t1, _T2&& __t2)
{
@@ -615,42 +626,42 @@
_LIBCPP_INLINE_VISIBILITY inline
constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
{
- return __get_pair<0>::get(__p);
+ return __get_pair<0>::get(__p);
}
template <class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY inline
constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
{
- return __get_pair<0>::get(__p);
+ return __get_pair<0>::get(__p);
}
template <class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY inline
constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
{
- return __get_pair<0>::get(_VSTD::move(__p));
+ return __get_pair<0>::get(_VSTD::move(__p));
}
template <class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY inline
constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
{
- return __get_pair<1>::get(__p);
+ return __get_pair<1>::get(__p);
}
template <class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY inline
constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
{
- return __get_pair<1>::get(__p);
+ return __get_pair<1>::get(__p);
}
template <class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY inline
constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
{
- return __get_pair<1>::get(_VSTD::move(__p));
+ return __get_pair<1>::get(_VSTD::move(__p));
}
#endif
diff --git a/test/utilities/utility/pairs/pair.astuple/get_const.pass.cpp b/test/utilities/utility/pairs/pair.astuple/get_const.pass.cpp
index a9c4481..43e70ea 100644
--- a/test/utilities/utility/pairs/pair.astuple/get_const.pass.cpp
+++ b/test/utilities/utility/pairs/pair.astuple/get_const.pass.cpp
@@ -26,4 +26,15 @@
assert(std::get<0>(p) == 3);
assert(std::get<1>(p) == 4);
}
+
+#if __cplusplus > 201103L
+ {
+ 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, "");
+ }
+#endif
}
diff --git a/test/utilities/utility/pairs/pairs.pair/const_first_const_second.pass.cpp b/test/utilities/utility/pairs/pairs.pair/const_first_const_second.pass.cpp
index de3a864..2041b39 100644
--- a/test/utilities/utility/pairs/pairs.pair/const_first_const_second.pass.cpp
+++ b/test/utilities/utility/pairs/pairs.pair/const_first_const_second.pass.cpp
@@ -22,9 +22,20 @@
public:
A(int data) : data_(data) {}
- bool operator==(const A& a) {return data_ == a.data_;}
+ bool operator==(const A& a) const {return data_ == a.data_;}
};
+#if _LIBCPP_STD_VER > 11
+class AC
+{
+ int data_;
+public:
+ constexpr AC(int data) : data_(data) {}
+
+ constexpr bool operator==(const AC& a) const {return data_ == a.data_;}
+};
+#endif
+
int main()
{
{
@@ -39,4 +50,19 @@
assert(p.first == A(1));
assert(p.second == 2);
}
+
+#if _LIBCPP_STD_VER > 11
+ {
+ typedef std::pair<float, short*> P;
+ constexpr P p(3.5f, 0);
+ static_assert(p.first == 3.5f, "");
+ static_assert(p.second == nullptr, "");
+ }
+ {
+ typedef std::pair<AC, int> P;
+ constexpr P p(1, 2);
+ static_assert(p.first == AC(1), "");
+ static_assert(p.second == 2, "");
+ }
+#endif
}
diff --git a/test/utilities/utility/pairs/pairs.pair/const_pair_U_V.pass.cpp b/test/utilities/utility/pairs/pairs.pair/const_pair_U_V.pass.cpp
index bcb3e53..286cce4 100644
--- a/test/utilities/utility/pairs/pairs.pair/const_pair_U_V.pass.cpp
+++ b/test/utilities/utility/pairs/pairs.pair/const_pair_U_V.pass.cpp
@@ -26,4 +26,15 @@
assert(p2.first == 3);
assert(p2.second == 4);
}
+
+#if _LIBCPP_STD_VER > 11
+ {
+ typedef std::pair<int, short> P1;
+ typedef std::pair<double, long> P2;
+ constexpr P1 p1(3, 4);
+ constexpr P2 p2 = p1;
+ static_assert(p2.first == 3, "");
+ static_assert(p2.second == 4, "");
+ }
+#endif
}
diff --git a/test/utilities/utility/pairs/pairs.pair/copy_ctor.pass.cpp b/test/utilities/utility/pairs/pairs.pair/copy_ctor.pass.cpp
index 433b0ae..b163f24 100644
--- a/test/utilities/utility/pairs/pairs.pair/copy_ctor.pass.cpp
+++ b/test/utilities/utility/pairs/pairs.pair/copy_ctor.pass.cpp
@@ -25,4 +25,14 @@
assert(p2.first == 3);
assert(p2.second == 4);
}
+
+#if _LIBCPP_STD_VER > 11
+ {
+ typedef std::pair<int, short> P1;
+ constexpr P1 p1(3, 4);
+ constexpr P1 p2 = p1;
+ static_assert(p2.first == 3, "");
+ static_assert(p2.second == 4, "");
+ }
+#endif
}
diff --git a/test/utilities/utility/pairs/pairs.pair/default.pass.cpp b/test/utilities/utility/pairs/pairs.pair/default.pass.cpp
index 644779e..18fdb47 100644
--- a/test/utilities/utility/pairs/pairs.pair/default.pass.cpp
+++ b/test/utilities/utility/pairs/pairs.pair/default.pass.cpp
@@ -18,8 +18,19 @@
int main()
{
+ {
typedef std::pair<float, short*> P;
P p;
assert(p.first == 0.0f);
assert(p.second == nullptr);
+ }
+
+#if _LIBCPP_STD_VER > 11
+ {
+ typedef std::pair<float, short*> P;
+ constexpr P p;
+ static_assert(p.first == 0.0f, "");
+ static_assert(p.second == nullptr, "");
+ }
+#endif
}
diff --git a/test/utilities/utility/pairs/pairs.spec/comparison.pass.cpp b/test/utilities/utility/pairs/pairs.spec/comparison.pass.cpp
index 821f4cd..9ba8532 100644
--- a/test/utilities/utility/pairs/pairs.spec/comparison.pass.cpp
+++ b/test/utilities/utility/pairs/pairs.spec/comparison.pass.cpp
@@ -78,4 +78,18 @@
assert( (p1 > p2));
assert( (p1 >= p2));
}
+
+#if _LIBCPP_STD_VER > 11
+ {
+ typedef std::pair<int, short> P;
+ constexpr P p1(3, 4);
+ constexpr P p2(3, 2);
+ static_assert(!(p1 == p2), "");
+ static_assert( (p1 != p2), "");
+ static_assert(!(p1 < p2), "");
+ static_assert(!(p1 <= p2), "");
+ static_assert( (p1 > p2), "");
+ static_assert( (p1 >= p2), "");
+ }
+#endif
}
diff --git a/test/utilities/utility/pairs/pairs.spec/make_pair.pass.cpp b/test/utilities/utility/pairs/pairs.spec/make_pair.pass.cpp
index a3d132f..48e0973 100644
--- a/test/utilities/utility/pairs/pairs.spec/make_pair.pass.cpp
+++ b/test/utilities/utility/pairs/pairs.spec/make_pair.pass.cpp
@@ -23,6 +23,7 @@
assert(p1.first == 3);
assert(p1.second == 4);
}
+
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
{
typedef std::pair<std::unique_ptr<int>, short> P1;
@@ -37,4 +38,14 @@
assert(p1.second == 4);
}
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+
+#if _LIBCPP_STD_VER > 11
+ {
+ typedef std::pair<int, short> P1;
+ constexpr P1 p1 = std::make_pair(3, 4);
+ static_assert(p1.first == 3, "");
+ static_assert(p1.second == 4, "");
+ }
+#endif
+
}