forward_as_tuple.pass.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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>
  10. // tuple<Types&&...> forward_as_tuple(Types&&... t);
  11. // UNSUPPORTED: c++98, c++03
  12. #include <tuple>
  13. #include <type_traits>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. template <class Tuple>
  17. void
  18. test0(const Tuple&)
  19. {
  20. static_assert(std::tuple_size<Tuple>::value == 0, "");
  21. }
  22. template <class Tuple>
  23. void
  24. test1a(const Tuple& t)
  25. {
  26. static_assert(std::tuple_size<Tuple>::value == 1, "");
  27. static_assert(std::is_same<typename std::tuple_element<0, Tuple>::type, int&&>::value, "");
  28. assert(std::get<0>(t) == 1);
  29. }
  30. template <class Tuple>
  31. void
  32. test1b(const Tuple& t)
  33. {
  34. static_assert(std::tuple_size<Tuple>::value == 1, "");
  35. static_assert(std::is_same<typename std::tuple_element<0, Tuple>::type, int&>::value, "");
  36. assert(std::get<0>(t) == 2);
  37. }
  38. template <class Tuple>
  39. void
  40. test2a(const Tuple& t)
  41. {
  42. static_assert(std::tuple_size<Tuple>::value == 2, "");
  43. static_assert(std::is_same<typename std::tuple_element<0, Tuple>::type, double&>::value, "");
  44. static_assert(std::is_same<typename std::tuple_element<1, Tuple>::type, char&>::value, "");
  45. assert(std::get<0>(t) == 2.5);
  46. assert(std::get<1>(t) == 'a');
  47. }
  48. #if TEST_STD_VER > 11
  49. template <class Tuple>
  50. constexpr int
  51. test3(const Tuple&)
  52. {
  53. return std::tuple_size<Tuple>::value;
  54. }
  55. #endif
  56. int main(int, char**)
  57. {
  58. {
  59. test0(std::forward_as_tuple());
  60. }
  61. {
  62. test1a(std::forward_as_tuple(1));
  63. }
  64. {
  65. int i = 2;
  66. test1b(std::forward_as_tuple(i));
  67. }
  68. {
  69. double i = 2.5;
  70. char c = 'a';
  71. test2a(std::forward_as_tuple(i, c));
  72. #if TEST_STD_VER > 11
  73. static_assert ( test3 (std::forward_as_tuple(i, c)) == 2, "" );
  74. #endif
  75. }
  76. return 0;
  77. }