xref: /llvm-project/libcxx/test/std/utilities/variant/variant.variant/variant.status/index.pass.cpp (revision 5c61b9d0cffc130dcb3949e9781823f2e8b5022d)
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 // The following compilers don't consider a type an aggregate type (and
14 // consequently not a literal type) if it has a base class at all.
15 // In C++17, an aggregate type is allowed to have a base class if it's not
16 // virtual, private, nor protected (e.g. ConstexprTestTypes:::NoCtors).
17 // XFAIL: gcc-5, gcc-6
18 // XFAIL: clang-3.5, clang-3.6, clang-3.7, clang-3.8
19 // XFAIL: apple-clang-6, apple-clang-7, apple-clang-8.0
20 
21 // <variant>
22 
23 // template <class ...Types> class variant;
24 
25 // constexpr size_t index() const noexcept;
26 
27 #include <cassert>
28 #include <string>
29 #include <type_traits>
30 #include <variant>
31 
32 #include "archetypes.hpp"
33 #include "test_macros.h"
34 #include "variant_test_helpers.hpp"
35 
36 int main() {
37 #if TEST_STD_VER == 17
38   { // This test does not pass on C++20 or later; see https://bugs.llvm.org/show_bug.cgi?id=39232
39     using V = std::variant<int, ConstexprTestTypes::NoCtors>;
40     constexpr V v;
41     static_assert(v.index() == 0, "");
42   }
43 #endif
44   {
45     using V = std::variant<int, long>;
46     constexpr V v(std::in_place_index<1>);
47     static_assert(v.index() == 1, "");
48   }
49   {
50     using V = std::variant<int, std::string>;
51     V v("abc");
52     assert(v.index() == 1);
53     v = 42;
54     assert(v.index() == 0);
55   }
56 #ifndef TEST_HAS_NO_EXCEPTIONS
57   {
58     using V = std::variant<int, MakeEmptyT>;
59     V v;
60     assert(v.index() == 0);
61     makeEmpty(v);
62     assert(v.index() == std::variant_npos);
63   }
64 #endif
65 }
66