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