xref: /llvm-project/libcxx/test/std/utilities/variant/variant.variant/variant.status/index.pass.cpp (revision d4b59a05fc7507cf69993109443dc5af47ae4fa8)
180e66ac1SEric Fiselier //===----------------------------------------------------------------------===//
280e66ac1SEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
680e66ac1SEric Fiselier //
780e66ac1SEric Fiselier //===----------------------------------------------------------------------===//
880e66ac1SEric Fiselier 
9*31cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
1080e66ac1SEric Fiselier 
1180e66ac1SEric Fiselier // <variant>
1280e66ac1SEric Fiselier 
1380e66ac1SEric Fiselier // template <class ...Types> class variant;
1480e66ac1SEric Fiselier 
1580e66ac1SEric Fiselier // constexpr size_t index() const noexcept;
1680e66ac1SEric Fiselier 
1780e66ac1SEric Fiselier #include <cassert>
1880e66ac1SEric Fiselier #include <string>
1980e66ac1SEric Fiselier #include <type_traits>
2080e66ac1SEric Fiselier #include <variant>
2180e66ac1SEric Fiselier 
22cc89063bSNico Weber #include "archetypes.h"
2380e66ac1SEric Fiselier #include "test_macros.h"
24cc89063bSNico Weber #include "variant_test_helpers.h"
2580e66ac1SEric Fiselier 
2620883fc2SLouis Dionne 
main(int,char **)272df59c50SJF Bastien int main(int, char**) {
2820883fc2SLouis Dionne   {
2920883fc2SLouis Dionne     using V = std::variant<int, long>;
3080e66ac1SEric Fiselier     constexpr V v;
3180e66ac1SEric Fiselier     static_assert(v.index() == 0, "");
3280e66ac1SEric Fiselier   }
3320883fc2SLouis Dionne   {
3420883fc2SLouis Dionne     using V = std::variant<int, long>;
3520883fc2SLouis Dionne     V v;
3620883fc2SLouis Dionne     assert(v.index() == 0);
3720883fc2SLouis Dionne   }
3880e66ac1SEric Fiselier   {
3980e66ac1SEric Fiselier     using V = std::variant<int, long>;
4080e66ac1SEric Fiselier     constexpr V v(std::in_place_index<1>);
4180e66ac1SEric Fiselier     static_assert(v.index() == 1, "");
4280e66ac1SEric Fiselier   }
4380e66ac1SEric Fiselier   {
4480e66ac1SEric Fiselier     using V = std::variant<int, std::string>;
4580e66ac1SEric Fiselier     V v("abc");
4680e66ac1SEric Fiselier     assert(v.index() == 1);
4780e66ac1SEric Fiselier     v = 42;
4880e66ac1SEric Fiselier     assert(v.index() == 0);
4980e66ac1SEric Fiselier   }
5080e66ac1SEric Fiselier #ifndef TEST_HAS_NO_EXCEPTIONS
5180e66ac1SEric Fiselier   {
5280e66ac1SEric Fiselier     using V = std::variant<int, MakeEmptyT>;
5380e66ac1SEric Fiselier     V v;
5480e66ac1SEric Fiselier     assert(v.index() == 0);
5580e66ac1SEric Fiselier     makeEmpty(v);
5680e66ac1SEric Fiselier     assert(v.index() == std::variant_npos);
5780e66ac1SEric Fiselier   }
5880e66ac1SEric Fiselier #endif
592df59c50SJF Bastien 
602df59c50SJF Bastien   return 0;
6180e66ac1SEric Fiselier }
62