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