xref: /llvm-project/libcxx/test/std/utilities/variant/variant.variant/variant.status/index.pass.cpp (revision 277799a2e95cd625e595da62cbad6df42a465bba)
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 // Clang 3.8 doesn't allow constexpr variables of non-literal type
14 // XFAIL: clang-3.8, apple-clang-7, apple-clang-8
15 
16 // <variant>
17 
18 // template <class ...Types> class variant;
19 
20 // constexpr size_t index() const noexcept;
21 
22 #include <cassert>
23 #include <string>
24 #include <type_traits>
25 #include <variant>
26 
27 #include "archetypes.hpp"
28 #include "test_macros.h"
29 #include "variant_test_helpers.hpp"
30 
31 int main() {
32   {
33     using V = std::variant<int, ConstexprTestTypes::NoCtors>;
34     constexpr V v;
35     static_assert(v.index() == 0, "");
36   }
37   {
38     using V = std::variant<int, long>;
39     constexpr V v(std::in_place_index<1>);
40     static_assert(v.index() == 1, "");
41   }
42   {
43     using V = std::variant<int, std::string>;
44     V v("abc");
45     assert(v.index() == 1);
46     v = 42;
47     assert(v.index() == 0);
48   }
49 #ifndef TEST_HAS_NO_EXCEPTIONS
50   {
51     using V = std::variant<int, MakeEmptyT>;
52     V v;
53     assert(v.index() == 0);
54     makeEmpty(v);
55     assert(v.index() == std::variant_npos);
56   }
57 #endif
58 }
59