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