// -*- C++ -*- //===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++98, c++03, c++11, c++14 // // template class variant; // template // T& emplace(initializer_list il,Args&&... args); #include #include #include #include #include "archetypes.hpp" #include "test_convertible.hpp" #include "test_macros.h" struct InitList { std::size_t size; constexpr InitList(std::initializer_list il) : size(il.size()) {} }; struct InitListArg { std::size_t size; int value; constexpr InitListArg(std::initializer_list il, int v) : size(il.size()), value(v) {} }; template constexpr auto test_emplace_exists_imp(int) -> decltype( std::declval().template emplace(std::declval()...), true) { return true; } template constexpr auto test_emplace_exists_imp(long) -> bool { return false; } template constexpr bool emplace_exists() { return test_emplace_exists_imp(0); } void test_emplace_sfinae() { using V = std::variant; using IL = std::initializer_list; static_assert(emplace_exists(), ""); static_assert(!emplace_exists(), "args don't match"); static_assert(!emplace_exists(), "too many args"); static_assert(emplace_exists(), ""); static_assert(!emplace_exists(), "args don't match"); static_assert(!emplace_exists(), "too few args"); static_assert(!emplace_exists(), "too many args"); } void test_basic() { using V = std::variant; V v; auto& ref1 = v.emplace({1, 2, 3}); static_assert(std::is_same_v, ""); assert(std::get(v).size == 3); assert(&ref1 == &std::get(v)); auto& ref2 = v.emplace({1, 2, 3, 4}, 42); static_assert(std::is_same_v, ""); assert(std::get(v).size == 4); assert(std::get(v).value == 42); assert(&ref2 == &std::get(v)); auto& ref3 = v.emplace({1}); static_assert(std::is_same_v, ""); assert(std::get(v).size == 1); assert(&ref3 == &std::get(v)); } int main() { test_basic(); test_emplace_sfinae(); }