diff options
author | Eric Fiselier <eric@efcs.ca> | 2016-07-25 02:36:42 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2016-07-25 02:36:42 +0000 |
commit | aedcbf898b7136b3aa09f236240eb5f1f05e4b78 (patch) | |
tree | b0e946a2c4b31f6034f4d07023468d4482040a94 /libcxx/test/std/utilities/utility | |
parent | 68623a0e9fbebfb2aa2edab710d021b26f3f1edf (diff) | |
download | bcm5719-llvm-aedcbf898b7136b3aa09f236240eb5f1f05e4b78.tar.gz bcm5719-llvm-aedcbf898b7136b3aa09f236240eb5f1f05e4b78.zip |
Recommit r276548 - Make pair/tuples assignment operators SFINAE properly.
I think I've solved issues with is_assignable and references to incomplete
types. The updated patch adds tests for this case.
llvm-svn: 276603
Diffstat (limited to 'libcxx/test/std/utilities/utility')
3 files changed, 216 insertions, 2 deletions
diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair.pass.cpp new file mode 100644 index 00000000000..3f706631000 --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair.pass.cpp @@ -0,0 +1,101 @@ +//===----------------------------------------------------------------------===// +// +// 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 T1, class T2> struct pair + +// pair& operator=(pair const& p); + +#include <utility> +#include <memory> +#include <cassert> + + +struct NonAssignable { + NonAssignable& operator=(NonAssignable const&) = delete; + NonAssignable& operator=(NonAssignable&&) = delete; +}; +struct CopyAssignable { + CopyAssignable() = default; + CopyAssignable(CopyAssignable const&) = default; + CopyAssignable& operator=(CopyAssignable const&) = default; + CopyAssignable& operator=(CopyAssignable&&) = delete; +}; +struct MoveAssignable { + MoveAssignable() = default; + MoveAssignable& operator=(MoveAssignable const&) = delete; + MoveAssignable& operator=(MoveAssignable&&) = default; +}; + +struct CountAssign { + static int copied; + static int moved; + static void reset() { copied = moved = 0; } + CountAssign() = default; + CountAssign& operator=(CountAssign const&) { ++copied; return *this; } + CountAssign& operator=(CountAssign&&) { ++moved; return *this; } +}; +int CountAssign::copied = 0; +int CountAssign::moved = 0; + +struct Incomplete; +extern Incomplete inc_obj; + +int main() +{ + { + typedef std::pair<CopyAssignable, short> P; + const P p1(CopyAssignable(), 4); + P p2; + p2 = p1; + assert(p2.second == 4); + } + { + using P = std::pair<int&, int&&>; + int x = 42; + int y = 101; + int x2 = -1; + int y2 = 300; + P p1(x, std::move(y)); + P p2(x2, std::move(y2)); + p1 = p2; + assert(p1.first == x2); + assert(p1.second == y2); + } + { + using P = std::pair<int, NonAssignable>; + static_assert(!std::is_copy_assignable<P>::value, ""); + } + { + CountAssign::reset(); + using P = std::pair<CountAssign, CopyAssignable>; + static_assert(std::is_copy_assignable<P>::value, ""); + P p; + P p2; + p = p2; + assert(CountAssign::copied == 1); + assert(CountAssign::moved == 0); + } + { + using P = std::pair<int, MoveAssignable>; + static_assert(!std::is_copy_assignable<P>::value, ""); + } + { + using P = std::pair<int, Incomplete&>; + static_assert(!std::is_copy_assignable<P>::value, ""); + P p(42, inc_obj); + assert(&p.second == &inc_obj); + } +} + +struct Incomplete {}; +Incomplete inc_obj; diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair_cxx03.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair_cxx03.pass.cpp new file mode 100644 index 00000000000..2623b800fff --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair_cxx03.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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-ANY: c++98, c++03 + +// <utility> + +// template <class T1, class T2> struct pair + +// pair& operator=(pair const& p); + +#include <utility> +#include <memory> +#include <cassert> + +struct NonAssignable { + NonAssignable() {} +private: + NonAssignable& operator=(NonAssignable const&); +}; + +struct Incomplete; +extern Incomplete inc_obj; + +int main() +{ + { + // Test that we don't constrain the assignment operator in C++03 mode. + // Since we don't have access control SFINAE having pair evaluate SFINAE + // may cause a hard error. + typedef std::pair<int, NonAssignable> P; + static_assert(std::is_copy_assignable<P>::value, ""); + } + { + typedef std::pair<int, Incomplete&> P; + static_assert(std::is_copy_assignable<P>::value, ""); + P p(42, inc_obj); + assert(&p.second == &inc_obj); + } +} + +struct Incomplete {}; +Incomplete inc_obj; diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_rv_pair.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_rv_pair.pass.cpp index a753ee520df..38089200e4d 100644 --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_rv_pair.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_rv_pair.pass.cpp @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03 + // <utility> // template <class T1, class T2> struct pair @@ -17,9 +19,35 @@ #include <memory> #include <cassert> + +struct NonAssignable { + NonAssignable& operator=(NonAssignable const&) = delete; + NonAssignable& operator=(NonAssignable&&) = delete; +}; +struct CopyAssignable { + CopyAssignable() = default; + CopyAssignable& operator=(CopyAssignable const&) = default; + CopyAssignable& operator=(CopyAssignable&&) = delete; +}; +struct MoveAssignable { + MoveAssignable() = default; + MoveAssignable& operator=(MoveAssignable const&) = delete; + MoveAssignable& operator=(MoveAssignable&&) = default; +}; + +struct CountAssign { + static int copied; + static int moved; + static void reset() { copied = moved = 0; } + CountAssign() = default; + CountAssign& operator=(CountAssign const&) { ++copied; return *this; } + CountAssign& operator=(CountAssign&&) { ++moved; return *this; } +}; +int CountAssign::copied = 0; +int CountAssign::moved = 0; + int main() { -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef std::pair<std::unique_ptr<int>, short> P; P p1(std::unique_ptr<int>(new int(3)), 4); @@ -28,5 +56,41 @@ int main() assert(*p2.first == 3); assert(p2.second == 4); } -#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + { + using P = std::pair<int&, int&&>; + int x = 42; + int y = 101; + int x2 = -1; + int y2 = 300; + P p1(x, std::move(y)); + P p2(x2, std::move(y2)); + p1 = std::move(p2); + assert(p1.first == x2); + assert(p1.second == y2); + } + { + using P = std::pair<int, NonAssignable>; + static_assert(!std::is_move_assignable<P>::value, ""); + } + { + // The move decays to the copy constructor + CountAssign::reset(); + using P = std::pair<CountAssign, CopyAssignable>; + static_assert(std::is_move_assignable<P>::value, ""); + P p; + P p2; + p = std::move(p2); + assert(CountAssign::moved == 0); + assert(CountAssign::copied == 1); + } + { + CountAssign::reset(); + using P = std::pair<CountAssign, MoveAssignable>; + static_assert(std::is_move_assignable<P>::value, ""); + P p; + P p2; + p = std::move(p2); + assert(CountAssign::moved == 1); + assert(CountAssign::copied == 0); + } } |