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