1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14
10
11// <variant>
12
13// template <class ...Types> class variant;
14
15// template <size_t I, class ...Args>
16// constexpr explicit variant(in_place_index_t<I>, Args&&...);
17
18#include <cassert>
19#include <string>
20#include <type_traits>
21#include <variant>
22
23#include "test_convertible.h"
24#include "test_macros.h"
25
26void test_ctor_sfinae() {
27 {
28 using V = std::variant<int>;
29 static_assert(
30 std::is_constructible<V, std::in_place_index_t<0>, int>::value, "");
31 static_assert(!test_convertible<V, std::in_place_index_t<0>, int>(), "");
32 }
33 {
34 using V = std::variant<int, long, long long>;
35 static_assert(
36 std::is_constructible<V, std::in_place_index_t<1>, int>::value, "");
37 static_assert(!test_convertible<V, std::in_place_index_t<1>, int>(), "");
38 }
39 {
40 using V = std::variant<int, long, int *>;
41 static_assert(
42 std::is_constructible<V, std::in_place_index_t<2>, int *>::value, "");
43 static_assert(!test_convertible<V, std::in_place_index_t<2>, int *>(), "");
44 }
45 { // args not convertible to type
46 using V = std::variant<int, long, int *>;
47 static_assert(
48 !std::is_constructible<V, std::in_place_index_t<0>, int *>::value, "");
49 static_assert(!test_convertible<V, std::in_place_index_t<0>, int *>(), "");
50 }
51 { // index not in variant
52 using V = std::variant<int, long, int *>;
53 static_assert(
54 !std::is_constructible<V, std::in_place_index_t<3>, int>::value, "");
55 static_assert(!test_convertible<V, std::in_place_index_t<3>, int>(), "");
56 }
57}
58
59void test_ctor_basic() {
60 {
61 constexpr std::variant<int> v(std::in_place_index<0>, 42);
62 static_assert(v.index() == 0, "");
63 static_assert(std::get<0>(v: v) == 42, "");
64 }
65 {
66 constexpr std::variant<int, long, long> v(std::in_place_index<1>, 42);
67 static_assert(v.index() == 1, "");
68 static_assert(std::get<1>(v: v) == 42, "");
69 }
70 {
71 constexpr std::variant<int, const int, long> v(std::in_place_index<1>, 42);
72 static_assert(v.index() == 1, "");
73 static_assert(std::get<1>(v: v) == 42, "");
74 }
75 {
76 using V = std::variant<const int, volatile int, int>;
77 int x = 42;
78 V v(std::in_place_index<0>, x);
79 assert(v.index() == 0);
80 assert(std::get<0>(v) == x);
81 }
82 {
83 using V = std::variant<const int, volatile int, int>;
84 int x = 42;
85 V v(std::in_place_index<1>, x);
86 assert(v.index() == 1);
87 assert(std::get<1>(v) == x);
88 }
89 {
90 using V = std::variant<const int, volatile int, int>;
91 int x = 42;
92 V v(std::in_place_index<2>, x);
93 assert(v.index() == 2);
94 assert(std::get<2>(v) == x);
95 }
96}
97
98int main(int, char**) {
99 test_ctor_basic();
100 test_ctor_sfinae();
101
102 return 0;
103}
104

source code of libcxx/test/std/utilities/variant/variant.variant/variant.ctor/in_place_index_args.pass.cpp