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// variant_alternative_t<I, variant<Types...>>& emplace(Args&&... args);
17
18#include <cassert>
19#include <string>
20#include <type_traits>
21#include <variant>
22
23#include "archetypes.h"
24#include "test_convertible.h"
25#include "test_macros.h"
26#include "variant_test_helpers.h"
27
28template <class Var, std::size_t I, class... Args>
29constexpr auto test_emplace_exists_imp(int) -> decltype(
30 std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
31 return true;
32}
33
34template <class, std::size_t, class...>
35constexpr auto test_emplace_exists_imp(long) -> bool {
36 return false;
37}
38
39template <class Var, std::size_t I, class... Args> constexpr bool emplace_exists() {
40 return test_emplace_exists_imp<Var, I, Args...>(0);
41}
42
43void test_emplace_sfinae() {
44 {
45 using V = std::variant<int, void *, const void *, TestTypes::NoCtors>;
46 static_assert(emplace_exists<V, 0>(), "");
47 static_assert(emplace_exists<V, 0, int>(), "");
48 static_assert(!emplace_exists<V, 0, decltype(nullptr)>(),
49 "cannot construct");
50 static_assert(emplace_exists<V, 1, decltype(nullptr)>(), "");
51 static_assert(emplace_exists<V, 1, int *>(), "");
52 static_assert(!emplace_exists<V, 1, const int *>(), "");
53 static_assert(!emplace_exists<V, 1, int>(), "cannot construct");
54 static_assert(emplace_exists<V, 2, const int *>(), "");
55 static_assert(emplace_exists<V, 2, int *>(), "");
56 static_assert(!emplace_exists<V, 3>(), "cannot construct");
57 }
58}
59
60void test_basic() {
61 {
62 using V = std::variant<int>;
63 V v(42);
64 auto& ref1 = v.emplace<0>();
65 static_assert(std::is_same_v<int&, decltype(ref1)>, "");
66 assert(std::get<0>(v) == 0);
67 assert(&ref1 == &std::get<0>(v));
68 auto& ref2 = v.emplace<0>(args: 42);
69 static_assert(std::is_same_v<int&, decltype(ref2)>, "");
70 assert(std::get<0>(v) == 42);
71 assert(&ref2 == &std::get<0>(v));
72 }
73 {
74 using V =
75 std::variant<int, long, const void *, TestTypes::NoCtors, std::string>;
76 const int x = 100;
77 V v(std::in_place_index<0>, -1);
78 // default emplace a value
79 auto& ref1 = v.emplace<1>();
80 static_assert(std::is_same_v<long&, decltype(ref1)>, "");
81 assert(std::get<1>(v) == 0);
82 assert(&ref1 == &std::get<1>(v));
83 auto& ref2 = v.emplace<2>(&x);
84 static_assert(std::is_same_v<const void*&, decltype(ref2)>, "");
85 assert(std::get<2>(v) == &x);
86 assert(&ref2 == &std::get<2>(v));
87 // emplace with multiple args
88 auto& ref3 = v.emplace<4>(3u, 'a');
89 static_assert(std::is_same_v<std::string&, decltype(ref3)>, "");
90 assert(std::get<4>(v) == "aaa");
91 assert(&ref3 == &std::get<4>(v));
92 }
93}
94
95int main(int, char**) {
96 test_basic();
97 test_emplace_sfinae();
98
99 return 0;
100}
101

source code of libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp