//===----------------------------------------------------------------------===// // // 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 // XFAIL: availability=macosx10.13 // XFAIL: availability=macosx10.12 // XFAIL: availability=macosx10.11 // XFAIL: availability=macosx10.10 // XFAIL: availability=macosx10.9 // XFAIL: availability=macosx10.8 // XFAIL: availability=macosx10.7 // // template T& emplace(Args&&...); // template // T& emplace(initializer_list, Args&&...); #include #include #include "any_helpers.h" #include "count_new.hpp" #include "test_macros.h" using std::any; using std::any_cast; struct Tracked { static int count; Tracked() {++count;} ~Tracked() { --count; } }; int Tracked::count = 0; template void test_emplace_type() { // constructing from a small type should perform no allocations. DisableAllocationGuard g(isSmallType()); ((void)g); assert(Type::count == 0); Type::reset(); { any a(std::in_place_type); assert(Tracked::count == 1); auto &v = a.emplace(); static_assert( std::is_same_v, "" ); assert(&v == std::any_cast(&a)); assert(Tracked::count == 0); assert(Type::count == 1); assert(Type::copied == 0); assert(Type::moved == 0); assertContains(a, 0); } assert(Type::count == 0); Type::reset(); { any a(std::in_place_type); assert(Tracked::count == 1); auto &v = a.emplace(101); static_assert( std::is_same_v, "" ); assert(&v == std::any_cast(&a)); assert(Tracked::count == 0); assert(Type::count == 1); assert(Type::copied == 0); assert(Type::moved == 0); assertContains(a, 101); } assert(Type::count == 0); Type::reset(); { any a(std::in_place_type); assert(Tracked::count == 1); auto &v = a.emplace(-1, 42, -1); static_assert( std::is_same_v, "" ); assert(&v == std::any_cast(&a)); assert(Tracked::count == 0); assert(Type::count == 1); assert(Type::copied == 0); assert(Type::moved == 0); assertContains(a, 42); } assert(Type::count == 0); Type::reset(); } template void test_emplace_type_tracked() { // constructing from a small type should perform no allocations. DisableAllocationGuard g(isSmallType()); ((void)g); { any a(std::in_place_type); assert(Tracked::count == 1); auto &v = a.emplace(); static_assert( std::is_same_v, "" ); assert(&v == std::any_cast(&a)); assert(Tracked::count == 0); assertArgsMatch(a); } { any a(std::in_place_type); assert(Tracked::count == 1); auto &v = a.emplace(-1, 42, -1); static_assert( std::is_same_v, "" ); assert(&v == std::any_cast(&a)); assert(Tracked::count == 0); assertArgsMatch(a); } // initializer_list constructor tests { any a(std::in_place_type); assert(Tracked::count == 1); auto &v = a.emplace({-1, 42, -1}); static_assert( std::is_same_v, "" ); assert(&v == std::any_cast(&a)); assert(Tracked::count == 0); assertArgsMatch>(a); } { int x = 42; any a(std::in_place_type); assert(Tracked::count == 1); auto &v = a.emplace({-1, 42, -1}, x); static_assert( std::is_same_v, "" ); assert(&v == std::any_cast(&a)); assert(Tracked::count == 0); assertArgsMatch, int&>(a); } } #ifndef TEST_HAS_NO_EXCEPTIONS struct SmallThrows { SmallThrows(int) { throw 42; } SmallThrows(std::initializer_list, int) { throw 42; } }; static_assert(IsSmallObject::value, ""); struct LargeThrows { LargeThrows(int) { throw 42; } LargeThrows(std::initializer_list, int) { throw 42; } int data[sizeof(std::any)]; }; static_assert(!IsSmallObject::value, ""); template void test_emplace_throws() { // any stores small type { std::any a(small{42}); assert(small::count == 1); try { auto &v = a.emplace(101); static_assert( std::is_same_v, "" ); assert(false); } catch (int const&) { } assert(small::count == 0); } { std::any a(small{42}); assert(small::count == 1); try { auto &v = a.emplace({1, 2, 3}, 101); static_assert( std::is_same_v, "" ); assert(false); } catch (int const&) { } assert(small::count == 0); } // any stores large type { std::any a(large{42}); assert(large::count == 1); try { auto &v = a.emplace(101); static_assert( std::is_same_v, "" ); assert(false); } catch (int const&) { } assert(large::count == 0); } { std::any a(large{42}); assert(large::count == 1); try { auto &v = a.emplace({1, 2, 3}, 101); static_assert( std::is_same_v, "" ); assert(false); } catch (int const&) { } assert(large::count == 0); } } #endif template constexpr auto has_emplace(int) -> decltype(std::any{}.emplace(std::declval()...), true) { return true; } template constexpr bool has_emplace(long) { return false; } template constexpr bool has_emplace() { return has_emplace(0); } template constexpr auto has_emplace_init_list(int) -> decltype(std::any{}.emplace( {std::declval(), std::declval(), std::declval()}, std::declval()...), true) { return true; } template constexpr bool has_emplace_init_list(long) { return false; } template constexpr bool has_emplace_init_list() { return has_emplace_init_list(0); } void test_emplace_sfinae_constraints() { { static_assert(has_emplace(), ""); static_assert(has_emplace(), ""); static_assert(!has_emplace(), "not constructible"); static_assert(!has_emplace_init_list(), "not constructible from il"); } { static_assert(has_emplace(), ""); static_assert(has_emplace(), ""); static_assert(!has_emplace(), ""); static_assert(!has_emplace(), ""); static_assert(has_emplace_init_list(), ""); static_assert(has_emplace_init_list(), ""); static_assert(!has_emplace_init_list(), ""); static_assert(!has_emplace_init_list(), ""); } { // Test that the emplace SFINAE's away when the // argument is non-copyable struct NoCopy { NoCopy() = default; NoCopy(NoCopy const&) = delete; NoCopy(int) {} NoCopy(std::initializer_list, int, int) {} }; static_assert(!has_emplace(), ""); static_assert(!has_emplace(), ""); static_assert(!has_emplace_init_list(), ""); static_assert(!has_emplace(), ""); static_assert(!has_emplace(), ""); static_assert(!has_emplace_init_list(), ""); static_assert(!has_emplace(), ""); static_assert(!has_emplace(), ""); static_assert(!has_emplace_init_list(), ""); } } int main() { test_emplace_type(); test_emplace_type(); test_emplace_type(); test_emplace_type(); test_emplace_type(); test_emplace_type_tracked(); test_emplace_type_tracked(); test_emplace_sfinae_constraints(); #ifndef TEST_HAS_NO_EXCEPTIONS test_emplace_throws(); test_emplace_throws(); #endif }