move.pass.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <tuple>
  9. // template <class... Types> class tuple;
  10. // tuple(tuple&& u);
  11. // UNSUPPORTED: c++98, c++03
  12. #include <tuple>
  13. #include <utility>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "MoveOnly.h"
  17. struct ConstructsWithTupleLeaf
  18. {
  19. ConstructsWithTupleLeaf() {}
  20. ConstructsWithTupleLeaf(ConstructsWithTupleLeaf const &) { assert(false); }
  21. ConstructsWithTupleLeaf(ConstructsWithTupleLeaf &&) {}
  22. template <class T>
  23. ConstructsWithTupleLeaf(T) {
  24. static_assert(!std::is_same<T, T>::value,
  25. "Constructor instantiated for type other than int");
  26. }
  27. };
  28. // move_only type which triggers the empty base optimization
  29. struct move_only_ebo {
  30. move_only_ebo() = default;
  31. move_only_ebo(move_only_ebo&&) = default;
  32. };
  33. // a move_only type which does not trigger the empty base optimization
  34. struct move_only_large final {
  35. move_only_large() : value(42) {}
  36. move_only_large(move_only_large&&) = default;
  37. int value;
  38. };
  39. template <class Elem>
  40. void test_sfinae() {
  41. using Tup = std::tuple<Elem>;
  42. using Alloc = std::allocator<void>;
  43. using Tag = std::allocator_arg_t;
  44. // special members
  45. {
  46. static_assert(std::is_default_constructible<Tup>::value, "");
  47. static_assert(std::is_move_constructible<Tup>::value, "");
  48. static_assert(!std::is_copy_constructible<Tup>::value, "");
  49. static_assert(!std::is_constructible<Tup, Tup&>::value, "");
  50. }
  51. // args constructors
  52. {
  53. static_assert(std::is_constructible<Tup, Elem&&>::value, "");
  54. static_assert(!std::is_constructible<Tup, Elem const&>::value, "");
  55. static_assert(!std::is_constructible<Tup, Elem&>::value, "");
  56. }
  57. // uses-allocator special member constructors
  58. {
  59. static_assert(std::is_constructible<Tup, Tag, Alloc>::value, "");
  60. static_assert(std::is_constructible<Tup, Tag, Alloc, Tup&&>::value, "");
  61. static_assert(!std::is_constructible<Tup, Tag, Alloc, Tup const&>::value, "");
  62. static_assert(!std::is_constructible<Tup, Tag, Alloc, Tup &>::value, "");
  63. }
  64. // uses-allocator args constructors
  65. {
  66. static_assert(std::is_constructible<Tup, Tag, Alloc, Elem&&>::value, "");
  67. static_assert(!std::is_constructible<Tup, Tag, Alloc, Elem const&>::value, "");
  68. static_assert(!std::is_constructible<Tup, Tag, Alloc, Elem &>::value, "");
  69. }
  70. }
  71. int main(int, char**)
  72. {
  73. {
  74. typedef std::tuple<> T;
  75. T t0;
  76. T t = std::move(t0);
  77. ((void)t); // Prevent unused warning
  78. }
  79. {
  80. typedef std::tuple<MoveOnly> T;
  81. T t0(MoveOnly(0));
  82. T t = std::move(t0);
  83. assert(std::get<0>(t) == 0);
  84. }
  85. {
  86. typedef std::tuple<MoveOnly, MoveOnly> T;
  87. T t0(MoveOnly(0), MoveOnly(1));
  88. T t = std::move(t0);
  89. assert(std::get<0>(t) == 0);
  90. assert(std::get<1>(t) == 1);
  91. }
  92. {
  93. typedef std::tuple<MoveOnly, MoveOnly, MoveOnly> T;
  94. T t0(MoveOnly(0), MoveOnly(1), MoveOnly(2));
  95. T t = std::move(t0);
  96. assert(std::get<0>(t) == 0);
  97. assert(std::get<1>(t) == 1);
  98. assert(std::get<2>(t) == 2);
  99. }
  100. // A bug in tuple caused __tuple_leaf to use its explicit converting constructor
  101. // as its move constructor. This tests that ConstructsWithTupleLeaf is not called
  102. // (w/ __tuple_leaf)
  103. {
  104. typedef std::tuple<ConstructsWithTupleLeaf> d_t;
  105. d_t d((ConstructsWithTupleLeaf()));
  106. d_t d2(static_cast<d_t &&>(d));
  107. }
  108. {
  109. test_sfinae<move_only_ebo>();
  110. test_sfinae<move_only_large>();
  111. }
  112. return 0;
  113. }