diff options
| author | Eric Fiselier <eric@efcs.ca> | 2016-07-25 04:32:07 +0000 |
|---|---|---|
| committer | Eric Fiselier <eric@efcs.ca> | 2016-07-25 04:32:07 +0000 |
| commit | 4927c295777f49ad518a809a85dc31c3e250680f (patch) | |
| tree | aa377dd8e9381c527153006a8dccd2316c2dfded /libcxx/test/std/utilities | |
| parent | 126de5d4b45d9fa2c55eaba761103cccd3113468 (diff) | |
| download | bcm5719-llvm-4927c295777f49ad518a809a85dc31c3e250680f.tar.gz bcm5719-llvm-4927c295777f49ad518a809a85dc31c3e250680f.zip | |
Implement the std::pair parts of "Improving pair and tuple". Completes N4387.
llvm-svn: 276605
Diffstat (limited to 'libcxx/test/std/utilities')
9 files changed, 668 insertions, 35 deletions
diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/U_V.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/U_V.pass.cpp index 8c7dee2499d..0598c2da51d 100644 --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/U_V.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/U_V.pass.cpp @@ -7,24 +7,93 @@ // //===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03 + // <utility> // template <class T1, class T2> struct pair // template<class U, class V> pair(U&& x, V&& y); + #include <utility> #include <memory> #include <cassert> +#include "archetypes.hpp" +#include "test_convertible.hpp" + +template <class T1, class T1Arg, + bool CanCopy = true, bool CanConvert = CanCopy> +void test_sfinae() { + using P1 = std::pair<T1, int>; + using P2 = std::pair<int, T1>; + using T2 = int const&; + static_assert(std::is_constructible<P1, T1Arg, T2>::value == CanCopy, ""); + static_assert(test_convertible<P1, T1Arg, T2>() == CanConvert, ""); + static_assert(std::is_constructible<P2, T2, T1Arg>::value == CanCopy, ""); + static_assert(test_convertible<P2, T2, T1Arg>() == CanConvert, ""); +} + +struct ExplicitT { + constexpr explicit ExplicitT(int x) : value(x) {} + int value; +}; + +struct ImplicitT { + constexpr ImplicitT(int x) : value(x) {} + int value; +}; + + int main() { -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef std::pair<std::unique_ptr<int>, short*> P; P p(std::unique_ptr<int>(new int(3)), nullptr); assert(*p.first == 3); assert(p.second == nullptr); } -#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + { + // Test non-const lvalue and rvalue types + test_sfinae<AllCtors, AllCtors&>(); + test_sfinae<AllCtors, AllCtors&&>(); + test_sfinae<ExplicitAllCtors, ExplicitAllCtors&, true, false>(); + test_sfinae<ExplicitAllCtors, ExplicitAllCtors&&, true, false>(); + test_sfinae<CopyOnly, CopyOnly&>(); + test_sfinae<CopyOnly, CopyOnly&&>(); + test_sfinae<ExplicitCopyOnly, ExplicitCopyOnly&, true, false>(); + test_sfinae<ExplicitCopyOnly, ExplicitCopyOnly&&, true, false>(); + test_sfinae<MoveOnly, MoveOnly&, false>(); + test_sfinae<MoveOnly, MoveOnly&&>(); + test_sfinae<ExplicitMoveOnly, ExplicitMoveOnly&, false>(); + test_sfinae<ExplicitMoveOnly, ExplicitMoveOnly&&, true, false>(); + test_sfinae<NonCopyable, NonCopyable&, false>(); + test_sfinae<NonCopyable, NonCopyable&&, false>(); + test_sfinae<ExplicitNonCopyable, ExplicitNonCopyable&, false>(); + test_sfinae<ExplicitNonCopyable, ExplicitNonCopyable&&, false>(); + } + { + // Test converting types + test_sfinae<ConvertingType, int&>(); + test_sfinae<ConvertingType, const int&>(); + test_sfinae<ConvertingType, int&&>(); + test_sfinae<ConvertingType, const int&&>(); + test_sfinae<ExplicitConvertingType, int&, true, false>(); + test_sfinae<ExplicitConvertingType, const int&, true, false>(); + test_sfinae<ExplicitConvertingType, int&&, true, false>(); + test_sfinae<ExplicitConvertingType, const int&&, true, false>(); + } +#if TEST_STD_VER > 11 + { // explicit constexpr test + constexpr std::pair<ExplicitT, ExplicitT> p(42, 43); + static_assert(p.first.value == 42, ""); + static_assert(p.second.value == 43, ""); + } + { // implicit constexpr test + constexpr std::pair<ImplicitT, ImplicitT> p = {42, 43}; + static_assert(p.first.value == 42, ""); + static_assert(p.second.value == 43, ""); + } +#endif } diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_first_const_second.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_first_const_second.pass.cpp index 2041b39c2dc..e3b780a0ec7 100644 --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_first_const_second.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_first_const_second.pass.cpp @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03 + // <utility> // template <class T1, class T2> struct pair @@ -16,25 +18,33 @@ #include <utility> #include <cassert> -class A -{ - int data_; -public: - A(int data) : data_(data) {} +#include "archetypes.hpp" +#include "test_convertible.hpp" - bool operator==(const A& a) const {return data_ == a.data_;} +struct ExplicitT { + constexpr explicit ExplicitT(int x) : value(x) {} + constexpr explicit ExplicitT(ExplicitT const& o) : value(o.value) {} + int value; }; -#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_;} +struct ImplicitT { + constexpr ImplicitT(int x) : value(x) {} + constexpr ImplicitT(ImplicitT const& o) : value(o.value) {} + int value; }; -#endif + +template <class T1, + bool CanCopy = true, bool CanConvert = CanCopy> +void test_sfinae() { + using P1 = std::pair<T1, int>; + using P2 = std::pair<int, T1>; + using T1Arg = T1 const&; + using T2 = int const&; + static_assert(std::is_constructible<P1, T1Arg, T2>::value == CanCopy, ""); + static_assert(test_convertible<P1, T1Arg, T2>() == CanConvert, ""); + static_assert(std::is_constructible<P2, T2, T1Arg>::value == CanCopy, ""); + static_assert(test_convertible<P2, T2, T1Arg>() == CanConvert, ""); +} int main() { @@ -45,13 +55,22 @@ int main() assert(p.second == nullptr); } { - typedef std::pair<A, int> P; + typedef std::pair<ImplicitT, int> P; P p(1, 2); - assert(p.first == A(1)); + assert(p.first.value == 1); assert(p.second == 2); } - -#if _LIBCPP_STD_VER > 11 + { + test_sfinae<AllCtors>(); + test_sfinae<ExplicitAllCtors, true, false>(); + test_sfinae<CopyOnly>(); + test_sfinae<ExplicitCopyOnly, true, false>(); + test_sfinae<MoveOnly, false>(); + test_sfinae<ExplicitMoveOnly, false>(); + test_sfinae<NonCopyable, false>(); + test_sfinae<ExplicitNonCopyable, false>(); + } +#if TEST_STD_VER > 11 { typedef std::pair<float, short*> P; constexpr P p(3.5f, 0); @@ -59,10 +78,20 @@ int main() 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, ""); + using P = std::pair<ExplicitT, int>; + constexpr ExplicitT e(42); + constexpr int x = 10; + constexpr P p(e, x); + static_assert(p.first.value == 42, ""); + static_assert(p.second == 10, ""); + } + { + using P = std::pair<ImplicitT, int>; + constexpr ImplicitT e(42); + constexpr int x = 10; + constexpr P p = {e, x}; + static_assert(p.first.value == 42, ""); + static_assert(p.second == 10, ""); } #endif } diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_first_const_second_cxx03.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_first_const_second_cxx03.pass.cpp new file mode 100644 index 00000000000..8c56c200346 --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_first_const_second_cxx03.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <utility> + +// template <class T1, class T2> struct pair + +// pair(const T1& x, const T2& y); + +#include <utility> +#include <cassert> + +class A +{ + int data_; +public: + A(int data) : data_(data) {} + + bool operator==(const A& a) const {return data_ == a.data_;} +}; + +int main() +{ + { + typedef std::pair<float, short*> P; + P p(3.5f, 0); + assert(p.first == 3.5f); + assert(p.second == nullptr); + } + { + typedef std::pair<A, int> P; + P p(1, 2); + assert(p.first == A(1)); + assert(p.second == 2); + } +} diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_pair_U_V.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_pair_U_V.pass.cpp index 286cce47f05..ce9c2ccd413 100644 --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_pair_U_V.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_pair_U_V.pass.cpp @@ -7,27 +7,151 @@ // //===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03 + // <utility> // template <class T1, class T2> struct pair -// template <class U, class V> pair(const pair<U, V>& p); +// template <class U, class V> EXPLICIT constexpr pair(const pair<U, V>& p); #include <utility> #include <cassert> +#include "archetypes.hpp" +#include "test_convertible.hpp" + +template <class T1, class U1, + bool CanCopy = true, bool CanConvert = CanCopy> +void test_pair_const() +{ + using P1 = std::pair<T1, int>; + using P2 = std::pair<int, T1>; + using UP1 = std::pair<U1, int> const&; + using UP2 = std::pair<int, U1> const&; + static_assert(std::is_constructible<P1, UP1>::value == CanCopy, ""); + static_assert(test_convertible<P1, UP1>() == CanConvert, ""); + static_assert(std::is_constructible<P2, UP2>::value == CanCopy, ""); + static_assert(test_convertible<P2, UP2>() == CanConvert, ""); +} + +template <class T, class U> +struct DPair : public std::pair<T, U> { + using Base = std::pair<T, U>; + using Base::Base; +}; + +struct ExplicitT { + constexpr explicit ExplicitT(int x) : value(x) {} + constexpr explicit ExplicitT(ExplicitT const& o) : value(o.value) {} + int value; +}; + +struct ImplicitT { + constexpr ImplicitT(int x) : value(x) {} + constexpr ImplicitT(ImplicitT const& o) : value(o.value) {} + int value; +}; + int main() { { typedef std::pair<int, short> P1; typedef std::pair<double, long> P2; - P1 p1(3, 4); - P2 p2 = p1; + const P1 p1(3, 4); + const P2 p2 = p1; assert(p2.first == 3); assert(p2.second == 4); } + { + // We allow derived types to use this constructor + using P1 = DPair<long, long>; + using P2 = std::pair<int, int>; + P1 p1(42, 101); + P2 p2(p1); + assert(p2.first == 42); + assert(p2.second = 101); + } + { + test_pair_const<AllCtors, AllCtors>(); // copy construction + test_pair_const<AllCtors, AllCtors&>(); + test_pair_const<AllCtors, AllCtors&&>(); + test_pair_const<AllCtors, const AllCtors&>(); + test_pair_const<AllCtors, const AllCtors&&>(); + + test_pair_const<ExplicitAllCtors, ExplicitAllCtors>(); // copy construction + test_pair_const<ExplicitAllCtors, ExplicitAllCtors&, true, false>(); + test_pair_const<ExplicitAllCtors, ExplicitAllCtors&&, true, false>(); + test_pair_const<ExplicitAllCtors, const ExplicitAllCtors&, true, false>(); + test_pair_const<ExplicitAllCtors, const ExplicitAllCtors&&, true, false>(); + + test_pair_const<MoveOnly, MoveOnly, false>(); // copy construction + test_pair_const<MoveOnly, MoveOnly&, false>(); + test_pair_const<MoveOnly, MoveOnly&&, false>(); + + test_pair_const<ExplicitMoveOnly, ExplicitMoveOnly, false>(); // copy construction + test_pair_const<ExplicitMoveOnly, ExplicitMoveOnly&, false>(); + test_pair_const<ExplicitMoveOnly, ExplicitMoveOnly&&, false>(); + + test_pair_const<CopyOnly, CopyOnly>(); + test_pair_const<CopyOnly, CopyOnly&>(); + test_pair_const<CopyOnly, CopyOnly&&>(); + + test_pair_const<ExplicitCopyOnly, ExplicitCopyOnly>(); + test_pair_const<ExplicitCopyOnly, ExplicitCopyOnly&, true, false>(); + test_pair_const<ExplicitCopyOnly, ExplicitCopyOnly&&, true, false>(); -#if _LIBCPP_STD_VER > 11 + test_pair_const<NonCopyable, NonCopyable, false>(); + test_pair_const<NonCopyable, NonCopyable&, false>(); + test_pair_const<NonCopyable, NonCopyable&&, false>(); + test_pair_const<NonCopyable, const NonCopyable&, false>(); + test_pair_const<NonCopyable, const NonCopyable&&, false>(); + } + + { // Test construction of references + test_pair_const<NonCopyable&, NonCopyable&>(); + test_pair_const<NonCopyable&, NonCopyable&&>(); + test_pair_const<NonCopyable&, NonCopyable const&, false>(); + test_pair_const<NonCopyable const&, NonCopyable&&>(); + test_pair_const<NonCopyable&&, NonCopyable&&, false>(); + + test_pair_const<ConvertingType&, int, false>(); + test_pair_const<ExplicitConvertingType&, int, false>(); + // Unfortunately the below conversions are allowed and create dangling + // references. + //test_pair_const<ConvertingType&&, int>(); + //test_pair_const<ConvertingType const&, int>(); + //test_pair_const<ConvertingType const&&, int>(); + // But these are not because the converting constructor is explicit. + test_pair_const<ExplicitConvertingType&&, int, false>(); + test_pair_const<ExplicitConvertingType const&, int, false>(); + test_pair_const<ExplicitConvertingType const&&, int, false>(); + + } + { + test_pair_const<AllCtors, int, false>(); + test_pair_const<ExplicitAllCtors, int, false>(); + test_pair_const<ConvertingType, int>(); + test_pair_const<ExplicitConvertingType, int, true, false>(); + + test_pair_const<ConvertingType, int>(); + test_pair_const<ConvertingType, ConvertingType>(); + test_pair_const<ConvertingType, ConvertingType const&>(); + test_pair_const<ConvertingType, ConvertingType&>(); + test_pair_const<ConvertingType, ConvertingType&&>(); + + test_pair_const<ExplicitConvertingType, int, true, false>(); + test_pair_const<ExplicitConvertingType, int&, true, false>(); + test_pair_const<ExplicitConvertingType, const int&, true, false>(); + test_pair_const<ExplicitConvertingType, int&&, true, false>(); + test_pair_const<ExplicitConvertingType, const int&&, true, false>(); + + test_pair_const<ExplicitConvertingType, ExplicitConvertingType>(); + test_pair_const<ExplicitConvertingType, ExplicitConvertingType const&, true, false>(); + test_pair_const<ExplicitConvertingType, ExplicitConvertingType&, true, false>(); + test_pair_const<ExplicitConvertingType, ExplicitConvertingType&&, true, false>(); + } +#if TEST_STD_VER > 11 { typedef std::pair<int, short> P1; typedef std::pair<double, long> P2; @@ -36,5 +160,21 @@ int main() static_assert(p2.first == 3, ""); static_assert(p2.second == 4, ""); } + { + using P1 = std::pair<int, int>; + using P2 = std::pair<ExplicitT, ExplicitT>; + constexpr P1 p1(42, 101); + constexpr P2 p2(p1); + static_assert(p2.first.value == 42, ""); + static_assert(p2.second.value == 101, ""); + } + { + using P1 = std::pair<int, int>; + using P2 = std::pair<ImplicitT, ImplicitT>; + constexpr P1 p1(42, 101); + constexpr P2 p2 = p1; + static_assert(p2.first.value == 42, ""); + static_assert(p2.second.value == 101, ""); + } #endif } diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_pair_U_V_cxx03.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_pair_U_V_cxx03.pass.cpp new file mode 100644 index 00000000000..73a843f2a70 --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/const_pair_U_V_cxx03.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <utility> + +// template <class T1, class T2> struct pair + +// template <class U, class V> pair(const pair<U, V>& p); + +#include <utility> +#include <cassert> + +int main() +{ + { + typedef std::pair<int, short> P1; + typedef std::pair<double, long> P2; + const P1 p1(3, 4); + const P2 p2 = p1; + assert(p2.first == 3); + assert(p2.second == 4); + } +} diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/default.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/default.pass.cpp index 97182d24d02..e6965412539 100644 --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/default.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/default.pass.cpp @@ -27,14 +27,15 @@ #include <cassert> #include "test_macros.h" +#include "archetypes.hpp" int main() { { - typedef std::pair<float, short*> P; - P p; - assert(p.first == 0.0f); - assert(p.second == nullptr); + typedef std::pair<float, short*> P; + P p; + assert(p.first == 0.0f); + assert(p.second == nullptr); } #if TEST_STD_VER >= 11 { @@ -43,5 +44,11 @@ int main() static_assert(p.first == 0.0f, ""); static_assert(p.second == nullptr, ""); } + { + using P = std::pair<int, NoDefault>; + static_assert(!std::is_default_constructible<P>::value, ""); + using P2 = std::pair<NoDefault, int>; + static_assert(!std::is_default_constructible<P>::value, ""); + } #endif } diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/not_constexpr_cxx11.fail.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/not_constexpr_cxx11.fail.cpp new file mode 100644 index 00000000000..3704dcc32ed --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/not_constexpr_cxx11.fail.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// REQUIRES: c++11 + +// <utility> + +// Test that only the default constructor is constexpr in C++11 + +#include <utility> +#include <cassert> + +struct ExplicitT { + constexpr explicit ExplicitT(int x) : value(x) {} + constexpr explicit ExplicitT(ExplicitT const& o) : value(o.value) {} + int value; +}; + +struct ImplicitT { + constexpr ImplicitT(int x) : value(x) {} + constexpr ImplicitT(ImplicitT const& o) : value(o.value) {} + int value; +}; + +int main() +{ + { + using P = std::pair<int, int>; + constexpr int x = 42; + constexpr P default_p{}; + constexpr P copy_p(default_p); + constexpr P const_U_V(x, x); // expected-error {{must be initialized by a constant expression}} + constexpr P U_V(42, 101); // expected-error {{must be initialized by a constant expression}} + } + { + using P = std::pair<ExplicitT, ExplicitT>; + constexpr std::pair<int, int> other; + constexpr ExplicitT e(99); + constexpr P const_U_V(e, e); // expected-error {{must be initialized by a constant expression}} + constexpr P U_V(42, 101); // expected-error {{must be initialized by a constant expression}} + constexpr P pair_U_V(other); // expected-error {{must be initialized by a constant expression}} + } + { + using P = std::pair<ImplicitT, ImplicitT>; + constexpr std::pair<int, int> other; + constexpr ImplicitT i = 99; + constexpr P const_U_V = {i, i}; // expected-error {{must be initialized by a constant expression}} + constexpr P U_V = {42, 101}; // expected-error {{must be initialized by a constant expression}} + constexpr P pair_U_V = other; // expected-error {{must be initialized by a constant expression}} + } +} diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/rv_pair_U_V.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/rv_pair_U_V.pass.cpp index 5fb6c98979b..f10fae4f9ef 100644 --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/rv_pair_U_V.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/rv_pair_U_V.pass.cpp @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03 + // <utility> // template <class T1, class T2> struct pair @@ -17,6 +19,23 @@ #include <memory> #include <cassert> +#include "archetypes.hpp" +#include "test_convertible.hpp" + +template <class T1, class U1, + bool CanCopy = true, bool CanConvert = CanCopy> +void test_pair_rv() +{ + using P1 = std::pair<T1, int>; + using P2 = std::pair<int, T1>; + using UP1 = std::pair<U1, int>&&; + using UP2 = std::pair<int, U1>&&; + static_assert(std::is_constructible<P1, UP1>::value == CanCopy, ""); + static_assert(test_convertible<P1, UP1>() == CanConvert, ""); + static_assert(std::is_constructible<P2, UP2>::value == CanCopy, ""); + static_assert(test_convertible<P2, UP2>() == CanConvert, ""); +} + struct Base { virtual ~Base() {} @@ -27,9 +46,25 @@ struct Derived { }; + +template <class T, class U> +struct DPair : public std::pair<T, U> { + using Base = std::pair<T, U>; + using Base::Base; +}; + +struct ExplicitT { + constexpr explicit ExplicitT(int x) : value(x) {} + int value; +}; + +struct ImplicitT { + constexpr ImplicitT(int x) : value(x) {} + int value; +}; + int main() { -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef std::pair<std::unique_ptr<Derived>, short> P1; typedef std::pair<std::unique_ptr<Base>, long> P2; @@ -38,5 +73,104 @@ int main() assert(p2.first == nullptr); assert(p2.second == 4); } -#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + { + // We allow derived types to use this constructor + using P1 = DPair<long, long>; + using P2 = std::pair<int, int>; + P1 p1(42, 101); + P2 p2(std::move(p1)); + assert(p2.first == 42); + assert(p2.second = 101); + } + { + test_pair_rv<AllCtors, AllCtors>(); + test_pair_rv<AllCtors, AllCtors&>(); + test_pair_rv<AllCtors, AllCtors&&>(); + test_pair_rv<AllCtors, const AllCtors&>(); + test_pair_rv<AllCtors, const AllCtors&&>(); + + test_pair_rv<ExplicitAllCtors, ExplicitAllCtors>(); + test_pair_rv<ExplicitAllCtors, ExplicitAllCtors&, true, false>(); + test_pair_rv<ExplicitAllCtors, ExplicitAllCtors&&, true, false>(); + test_pair_rv<ExplicitAllCtors, const ExplicitAllCtors&, true, false>(); + test_pair_rv<ExplicitAllCtors, const ExplicitAllCtors&&, true, false>(); + + test_pair_rv<MoveOnly, MoveOnly>(); + test_pair_rv<MoveOnly, MoveOnly&, false>(); + test_pair_rv<MoveOnly, MoveOnly&&>(); + + test_pair_rv<ExplicitMoveOnly, ExplicitMoveOnly>(); // copy construction + test_pair_rv<ExplicitMoveOnly, ExplicitMoveOnly&, false>(); + test_pair_rv<ExplicitMoveOnly, ExplicitMoveOnly&&, true, false>(); + + test_pair_rv<CopyOnly, CopyOnly>(); + test_pair_rv<CopyOnly, CopyOnly&>(); + test_pair_rv<CopyOnly, CopyOnly&&>(); + + test_pair_rv<ExplicitCopyOnly, ExplicitCopyOnly>(); + test_pair_rv<ExplicitCopyOnly, ExplicitCopyOnly&, true, false>(); + test_pair_rv<ExplicitCopyOnly, ExplicitCopyOnly&&, true, false>(); + + test_pair_rv<NonCopyable, NonCopyable, false>(); + test_pair_rv<NonCopyable, NonCopyable&, false>(); + test_pair_rv<NonCopyable, NonCopyable&&, false>(); + test_pair_rv<NonCopyable, const NonCopyable&, false>(); + test_pair_rv<NonCopyable, const NonCopyable&&, false>(); + } + { // Test construction of references + test_pair_rv<NonCopyable&, NonCopyable&>(); + test_pair_rv<NonCopyable&, NonCopyable&&>(); + test_pair_rv<NonCopyable&, NonCopyable const&, false>(); + test_pair_rv<NonCopyable const&, NonCopyable&&>(); + test_pair_rv<NonCopyable&&, NonCopyable&&>(); + + test_pair_rv<ConvertingType&, int, false>(); + test_pair_rv<ExplicitConvertingType&, int, false>(); + // Unfortunately the below conversions are allowed and create dangling + // references. + //test_pair_rv<ConvertingType&&, int>(); + //test_pair_rv<ConvertingType const&, int>(); + //test_pair_rv<ConvertingType const&&, int>(); + // But these are not because the converting constructor is explicit. + test_pair_rv<ExplicitConvertingType&&, int, false>(); + test_pair_rv<ExplicitConvertingType const&, int, false>(); + test_pair_rv<ExplicitConvertingType const&&, int, false>(); + } + { + test_pair_rv<AllCtors, int, false>(); + test_pair_rv<ExplicitAllCtors, int, false>(); + test_pair_rv<ConvertingType, int>(); + test_pair_rv<ExplicitConvertingType, int, true, false>(); + + test_pair_rv<ConvertingType, int>(); + test_pair_rv<ConvertingType, ConvertingType>(); + test_pair_rv<ConvertingType, ConvertingType const&>(); + test_pair_rv<ConvertingType, ConvertingType&>(); + test_pair_rv<ConvertingType, ConvertingType&&>(); + + test_pair_rv<ExplicitConvertingType, int, true, false>(); + test_pair_rv<ExplicitConvertingType, int&, true, false>(); + test_pair_rv<ExplicitConvertingType, const int&, true, false>(); + test_pair_rv<ExplicitConvertingType, int&&, true, false>(); + test_pair_rv<ExplicitConvertingType, const int&&, true, false>(); + + test_pair_rv<ExplicitConvertingType, ExplicitConvertingType>(); + test_pair_rv<ExplicitConvertingType, ExplicitConvertingType const&, true, false>(); + test_pair_rv<ExplicitConvertingType, ExplicitConvertingType&, true, false>(); + test_pair_rv<ExplicitConvertingType, ExplicitConvertingType&&, true, false>(); + } +#if TEST_STD_VER > 11 + { // explicit constexpr test + constexpr std::pair<int, int> p1(42, 43); + constexpr std::pair<ExplicitT, ExplicitT> p2(std::move(p1)); + static_assert(p2.first.value == 42, ""); + static_assert(p2.second.value == 43, ""); + } + { // implicit constexpr test + constexpr std::pair<int, int> p1(42, 43); + constexpr std::pair<ImplicitT, ImplicitT> p2 = std::move(p1); + static_assert(p2.first.value == 42, ""); + static_assert(p2.second.value == 43, ""); + } +#endif } diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/special_member_generation_test.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/special_member_generation_test.pass.cpp new file mode 100644 index 00000000000..95cdfadc916 --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/special_member_generation_test.pass.cpp @@ -0,0 +1,126 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <utility> + +// template <class T, class U> struct pair; + +// pair(pair const&) = default; +// pair(pair &&) = default; +// pair& operator=(pair const&); +// pair& operator=(pair&&); + +// Test that the copy/move constructors and assignment operators are +// correctly defined or deleted based on the properties of `T` and `U`. + +#include <cassert> +#include <string> +#include <tuple> + +#include "archetypes.hpp" + +namespace ConstructorTest { + +template <class T1, bool CanCopy = true, bool CanMove = CanCopy> void test() { + using P1 = std::pair<T1, int>; + using P2 = std::pair<int, T1>; + static_assert(std::is_copy_constructible<P1>::value == CanCopy); + static_assert(std::is_move_constructible<P1>::value == CanMove); + static_assert(std::is_copy_constructible<P2>::value == CanCopy); + static_assert(std::is_move_constructible<P2>::value == CanMove); +}; + +} // namespace ConstructorTest + +void test_constructors_exist() { + using namespace ConstructorTest; + { + test<int>(); + test<int &>(); + test<int &&, false, true>(); + test<const int>(); + test<const int &>(); + test<const int &&, false, true>(); + } + { + test<Copyable>(); + test<Copyable &>(); + test<Copyable &&, false, true>(); + } + { + test<NonCopyable, false>(); + test<NonCopyable &, true>(); + test<NonCopyable &&, false, true>(); + } + { + // Even though CopyOnly has an explicitly deleted move constructor + // pair's move constructor is only implicitly deleted and therefore + // it doesn't participate in overload resolution. + test<CopyOnly, true, true>(); + test<CopyOnly &, true>(); + test<CopyOnly &&, false, true>(); + } + { + test<MoveOnly, false, true>(); + test<MoveOnly &, true>(); + test<MoveOnly &&, false, true>(); + } +} + +namespace AssignmentOperatorTest { + +template <class T1, bool CanCopy = true, bool CanMove = CanCopy> void test() { + using P1 = std::pair<T1, int>; + using P2 = std::pair<int, T1>; + static_assert(std::is_copy_assignable<P1>::value == CanCopy); + static_assert(std::is_move_assignable<P1>::value == CanMove); + static_assert(std::is_copy_assignable<P2>::value == CanCopy); + static_assert(std::is_move_assignable<P2>::value == CanMove); +}; + +} // namespace AssignmentOperatorTest + +void test_assignment_operator_exists() { + using namespace AssignmentOperatorTest; + { + test<int>(); + test<int &>(); + test<int &&>(); + test<const int, false>(); + test<const int &, false>(); + test<const int &&, false>(); + } + { + test<Copyable>(); + test<Copyable &>(); + test<Copyable &&>(); + } + { + test<NonCopyable, false>(); + test<NonCopyable &, false>(); + test<NonCopyable &&, false>(); + } + { + test<CopyOnly, true>(); + test<CopyOnly &, true>(); + test<CopyOnly &&, true>(); + } + { + test<MoveOnly, false, true>(); + test<MoveOnly &, false, false>(); + test<MoveOnly &&, false, true>(); + } +} + +int main() { + test_constructors_exist(); + test_assignment_operator_exists(); +} |

