index.pass.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  11. // <variant>
  12. // template <class ...Types> class variant;
  13. // constexpr size_t index() const noexcept;
  14. #include <cassert>
  15. #include <string>
  16. #include <type_traits>
  17. #include <variant>
  18. #include "archetypes.hpp"
  19. #include "test_macros.h"
  20. #include "variant_test_helpers.hpp"
  21. int main() {
  22. {
  23. using V = std::variant<int, ConstexprTestTypes::NoCtors>;
  24. constexpr V v;
  25. static_assert(v.index() == 0, "");
  26. }
  27. {
  28. using V = std::variant<int, long>;
  29. constexpr V v(std::in_place_index<1>);
  30. static_assert(v.index() == 1, "");
  31. }
  32. {
  33. using V = std::variant<int, std::string>;
  34. V v("abc");
  35. assert(v.index() == 1);
  36. v = 42;
  37. assert(v.index() == 0);
  38. }
  39. #ifndef TEST_HAS_NO_EXCEPTIONS
  40. {
  41. using V = std::variant<int, MakeEmptyT>;
  42. V v;
  43. assert(v.index() == 0);
  44. makeEmpty(v);
  45. assert(v.index() == std::variant_npos);
  46. }
  47. #endif
  48. }