emplace_index_args.pass.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // XFAIL: dylib-has-no-bad_variant_access && !libcpp-no-exceptions
  11. // <variant>
  12. // template <class ...Types> class variant;
  13. // template <size_t I, class ...Args>
  14. // variant_alternative_t<I, variant<Types...>>& emplace(Args&&... args);
  15. #include <cassert>
  16. #include <string>
  17. #include <type_traits>
  18. #include <variant>
  19. #include "archetypes.h"
  20. #include "test_convertible.h"
  21. #include "test_macros.h"
  22. #include "variant_test_helpers.h"
  23. template <class Var, size_t I, class... Args>
  24. constexpr auto test_emplace_exists_imp(int) -> decltype(
  25. std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
  26. return true;
  27. }
  28. template <class, size_t, class...>
  29. constexpr auto test_emplace_exists_imp(long) -> bool {
  30. return false;
  31. }
  32. template <class Var, size_t I, class... Args> constexpr bool emplace_exists() {
  33. return test_emplace_exists_imp<Var, I, Args...>(0);
  34. }
  35. void test_emplace_sfinae() {
  36. {
  37. using V = std::variant<int, void *, const void *, TestTypes::NoCtors>;
  38. static_assert(emplace_exists<V, 0>(), "");
  39. static_assert(emplace_exists<V, 0, int>(), "");
  40. static_assert(!emplace_exists<V, 0, decltype(nullptr)>(),
  41. "cannot construct");
  42. static_assert(emplace_exists<V, 1, decltype(nullptr)>(), "");
  43. static_assert(emplace_exists<V, 1, int *>(), "");
  44. static_assert(!emplace_exists<V, 1, const int *>(), "");
  45. static_assert(!emplace_exists<V, 1, int>(), "cannot construct");
  46. static_assert(emplace_exists<V, 2, const int *>(), "");
  47. static_assert(emplace_exists<V, 2, int *>(), "");
  48. static_assert(!emplace_exists<V, 3>(), "cannot construct");
  49. }
  50. #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
  51. {
  52. using V = std::variant<int, int &, const int &, int &&, TestTypes::NoCtors>;
  53. static_assert(emplace_exists<V, 0>(), "");
  54. static_assert(emplace_exists<V, 0, int>(), "");
  55. static_assert(emplace_exists<V, 0, long long>(), "");
  56. static_assert(!emplace_exists<V, 0, int, int>(), "too many args");
  57. static_assert(emplace_exists<V, 1, int &>(), "");
  58. static_assert(!emplace_exists<V, 1>(), "cannot default construct ref");
  59. static_assert(!emplace_exists<V, 1, const int &>(), "cannot bind ref");
  60. static_assert(!emplace_exists<V, 1, int &&>(), "cannot bind ref");
  61. static_assert(emplace_exists<V, 2, int &>(), "");
  62. static_assert(emplace_exists<V, 2, const int &>(), "");
  63. static_assert(emplace_exists<V, 2, int &&>(), "");
  64. static_assert(!emplace_exists<V, 2, void *>(),
  65. "not constructible from void*");
  66. static_assert(emplace_exists<V, 3, int>(), "");
  67. static_assert(!emplace_exists<V, 3, int &>(), "cannot bind ref");
  68. static_assert(!emplace_exists<V, 3, const int &>(), "cannot bind ref");
  69. static_assert(!emplace_exists<V, 3, const int &&>(), "cannot bind ref");
  70. static_assert(!emplace_exists<V, 4>(), "no ctors");
  71. }
  72. #endif
  73. }
  74. void test_basic() {
  75. {
  76. using V = std::variant<int>;
  77. V v(42);
  78. auto& ref1 = v.emplace<0>();
  79. static_assert(std::is_same_v<int&, decltype(ref1)>, "");
  80. assert(std::get<0>(v) == 0);
  81. assert(&ref1 == &std::get<0>(v));
  82. auto& ref2 = v.emplace<0>(42);
  83. static_assert(std::is_same_v<int&, decltype(ref2)>, "");
  84. assert(std::get<0>(v) == 42);
  85. assert(&ref2 == &std::get<0>(v));
  86. }
  87. {
  88. using V =
  89. std::variant<int, long, const void *, TestTypes::NoCtors, std::string>;
  90. const int x = 100;
  91. V v(std::in_place_index<0>, -1);
  92. // default emplace a value
  93. auto& ref1 = v.emplace<1>();
  94. static_assert(std::is_same_v<long&, decltype(ref1)>, "");
  95. assert(std::get<1>(v) == 0);
  96. assert(&ref1 == &std::get<1>(v));
  97. auto& ref2 = v.emplace<2>(&x);
  98. static_assert(std::is_same_v<const void*&, decltype(ref2)>, "");
  99. assert(std::get<2>(v) == &x);
  100. assert(&ref2 == &std::get<2>(v));
  101. // emplace with multiple args
  102. auto& ref3 = v.emplace<4>(3, 'a');
  103. static_assert(std::is_same_v<std::string&, decltype(ref3)>, "");
  104. assert(std::get<4>(v) == "aaa");
  105. assert(&ref3 == &std::get<4>(v));
  106. }
  107. #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
  108. {
  109. using V = std::variant<int, long, const int &, int &&, TestTypes::NoCtors,
  110. std::string>;
  111. const int x = 100;
  112. int y = 42;
  113. int z = 43;
  114. V v(std::in_place_index<0>, -1);
  115. // default emplace a value
  116. auto& ref1 = v.emplace<1>();
  117. static_assert(std::is_same_v<long&, decltype(ref1)>, "");
  118. assert(std::get<1>(v) == 0);
  119. assert(&ref1 == &std::get<1>(v));
  120. // emplace a reference
  121. auto& ref2 = v.emplace<2>(x);
  122. static_assert(std::is_same_v<&, decltype(ref)>, "");
  123. assert(&std::get<2>(v) == &x);
  124. assert(&ref2 == &std::get<2>(v));
  125. // emplace an rvalue reference
  126. auto& ref3 = v.emplace<3>(std::move(y));
  127. static_assert(std::is_same_v<&, decltype(ref)>, "");
  128. assert(&std::get<3>(v) == &y);
  129. assert(&ref3 == &std::get<3>(v));
  130. // re-emplace a new reference over the active member
  131. auto& ref4 = v.emplace<3>(std::move(z));
  132. static_assert(std::is_same_v<&, decltype(ref)>, "");
  133. assert(&std::get<3>(v) == &z);
  134. assert(&ref4 == &std::get<3>(v));
  135. // emplace with multiple args
  136. auto& ref5 = v.emplace<5>(3u, 'a');
  137. static_assert(std::is_same_v<std::string&, decltype(ref5)>, "");
  138. assert(std::get<5>(v) == "aaa");
  139. assert(&ref5 == &std::get<5>(v));
  140. }
  141. #endif
  142. }
  143. int main(int, char**) {
  144. test_basic();
  145. test_emplace_sfinae();
  146. return 0;
  147. }