variant_alternative.pass.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // <variant>
  11. // template <size_t I, class T> struct variant_alternative; // undefined
  12. // template <size_t I, class T> struct variant_alternative<I, const T>;
  13. // template <size_t I, class T> struct variant_alternative<I, volatile T>;
  14. // template <size_t I, class T> struct variant_alternative<I, const volatile T>;
  15. // template <size_t I, class T>
  16. // using variant_alternative_t = typename variant_alternative<I, T>::type;
  17. //
  18. // template <size_t I, class... Types>
  19. // struct variant_alternative<I, variant<Types...>>;
  20. #include <memory>
  21. #include <type_traits>
  22. #include <variant>
  23. #include "test_macros.h"
  24. #include "variant_test_helpers.h"
  25. template <class V, size_t I, class E> void test() {
  26. static_assert(
  27. std::is_same_v<typename std::variant_alternative<I, V>::type, E>, "");
  28. static_assert(
  29. std::is_same_v<typename std::variant_alternative<I, const V>::type,
  30. const E>,
  31. "");
  32. static_assert(
  33. std::is_same_v<typename std::variant_alternative<I, volatile V>::type,
  34. volatile E>,
  35. "");
  36. static_assert(
  37. std::is_same_v<
  38. typename std::variant_alternative<I, const volatile V>::type,
  39. const volatile E>,
  40. "");
  41. static_assert(std::is_same_v<std::variant_alternative_t<I, V>, E>, "");
  42. static_assert(std::is_same_v<std::variant_alternative_t<I, const V>, const E>,
  43. "");
  44. static_assert(
  45. std::is_same_v<std::variant_alternative_t<I, volatile V>, volatile E>,
  46. "");
  47. static_assert(std::is_same_v<std::variant_alternative_t<I, const volatile V>,
  48. const volatile E>,
  49. "");
  50. }
  51. int main(int, char**) {
  52. {
  53. using V = std::variant<int, void *, const void *, long double>;
  54. test<V, 0, int>();
  55. test<V, 1, void *>();
  56. test<V, 2, const void *>();
  57. test<V, 3, long double>();
  58. }
  59. #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
  60. {
  61. using V = std::variant<int, int &, const int &, int &&, long double>;
  62. test<V, 0, int>();
  63. test<V, 1, int &>();
  64. test<V, 2, const int &>();
  65. test<V, 3, int &&>();
  66. test<V, 4, long double>();
  67. }
  68. #endif
  69. return 0;
  70. }