115 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 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)30 -> decltype(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>40constexpr bool emplace_exists() {41 return test_emplace_exists_imp<Var, I, Args...>(0);42}43 44constexpr void test_emplace_sfinae() {45 {46 using V = std::variant<int, void*, const void*, TestTypes::NoCtors>;47 static_assert(emplace_exists<V, 0>(), "");48 static_assert(emplace_exists<V, 0, int>(), "");49 static_assert(!emplace_exists<V, 0, decltype(nullptr)>(), "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 60struct NoCtor {61 NoCtor() = delete;62};63 64TEST_CONSTEXPR_CXX20 void test_basic() {65 {66 using V = std::variant<int>;67 V v(42);68 auto& ref1 = v.emplace<0>();69 static_assert(std::is_same_v<int&, decltype(ref1)>, "");70 assert(std::get<0>(v) == 0);71 assert(&ref1 == &std::get<0>(v));72 auto& ref2 = v.emplace<0>(42);73 static_assert(std::is_same_v<int&, decltype(ref2)>, "");74 assert(std::get<0>(v) == 42);75 assert(&ref2 == &std::get<0>(v));76 }77 78 {79 using V = std::variant<int, long, const void*, NoCtor, std::string>;80 const int x = 100;81 V v(std::in_place_index<0>, -1);82 // default emplace a value83 auto& ref1 = v.emplace<1>();84 static_assert(std::is_same_v<long&, decltype(ref1)>, "");85 assert(std::get<1>(v) == 0);86 assert(&ref1 == &std::get<1>(v));87 auto& ref2 = v.emplace<2>(&x);88 static_assert(std::is_same_v<const void*&, decltype(ref2)>, "");89 assert(std::get<2>(v) == &x);90 assert(&ref2 == &std::get<2>(v));91 // emplace with multiple args92 auto& ref3 = v.emplace<4>(3u, 'a');93 static_assert(std::is_same_v<std::string&, decltype(ref3)>, "");94 assert(std::get<4>(v) == "aaa");95 assert(&ref3 == &std::get<4>(v));96 }97}98 99TEST_CONSTEXPR_CXX20 bool test() {100 test_basic();101 test_emplace_sfinae();102 103 return true;104}105 106int main(int, char**) {107 test();108 109#if TEST_STD_VER >= 20110 static_assert(test());111#endif112 113 return 0;114}115