index.pass.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <class ...Types> class variant;
  12. // constexpr size_t index() const noexcept;
  13. #include <cassert>
  14. #include <string>
  15. #include <type_traits>
  16. #include <variant>
  17. #include "archetypes.h"
  18. #include "test_macros.h"
  19. #include "variant_test_helpers.h"
  20. int main(int, char**) {
  21. {
  22. using V = std::variant<int, long>;
  23. constexpr V v;
  24. static_assert(v.index() == 0, "");
  25. }
  26. {
  27. using V = std::variant<int, long>;
  28. V v;
  29. assert(v.index() == 0);
  30. }
  31. {
  32. using V = std::variant<int, long>;
  33. constexpr V v(std::in_place_index<1>);
  34. static_assert(v.index() == 1, "");
  35. }
  36. {
  37. using V = std::variant<int, std::string>;
  38. V v("abc");
  39. assert(v.index() == 1);
  40. v = 42;
  41. assert(v.index() == 0);
  42. }
  43. #ifndef TEST_HAS_NO_EXCEPTIONS
  44. {
  45. using V = std::variant<int, MakeEmptyT>;
  46. V v;
  47. assert(v.index() == 0);
  48. makeEmpty(v);
  49. assert(v.index() == std::variant_npos);
  50. }
  51. #endif
  52. return 0;
  53. }