1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// Throwing bad_any_cast is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// <any>
// template <class T, class ...Args> T& emplace(Args&&...);
// template <class T, class U, class ...Args>
// T& emplace(initializer_list<U>, Args&&...);
#include <any>
#include <cassert>
#include "any_helpers.h"
#include "count_new.h"
#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 <class Type>
void test_emplace_type() {
// constructing from a small type should perform no allocations.
DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
assert(Type::count == 0);
Type::reset();
{
any a(std::in_place_type<Tracked>);
assert(Tracked::count == 1);
auto &v = a.emplace<Type>();
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(&v == std::any_cast<Type>(&a));
assert(Tracked::count == 0);
assert(Type::count == 1);
assert(Type::copied == 0);
assert(Type::moved == 0);
assertContains<Type>(a, 0);
}
assert(Type::count == 0);
Type::reset();
{
any a(std::in_place_type<Tracked>);
assert(Tracked::count == 1);
auto &v = a.emplace<Type>(101);
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(&v == std::any_cast<Type>(&a));
assert(Tracked::count == 0);
assert(Type::count == 1);
assert(Type::copied == 0);
assert(Type::moved == 0);
assertContains<Type>(a, 101);
}
assert(Type::count == 0);
Type::reset();
{
any a(std::in_place_type<Tracked>);
assert(Tracked::count == 1);
auto &v = a.emplace<Type>(-1, 42, -1);
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(&v == std::any_cast<Type>(&a));
assert(Tracked::count == 0);
assert(Type::count == 1);
assert(Type::copied == 0);
assert(Type::moved == 0);
assertContains<Type>(a, 42);
}
assert(Type::count == 0);
Type::reset();
}
template <class Type>
void test_emplace_type_tracked() {
// constructing from a small type should perform no allocations.
DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
{
any a(std::in_place_type<Tracked>);
assert(Tracked::count == 1);
auto &v = a.emplace<Type>();
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(&v == std::any_cast<Type>(&a));
assert(Tracked::count == 0);
assertArgsMatch<Type>(a);
}
{
any a(std::in_place_type<Tracked>);
assert(Tracked::count == 1);
auto &v = a.emplace<Type>(-1, 42, -1);
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(&v == std::any_cast<Type>(&a));
assert(Tracked::count == 0);
assertArgsMatch<Type, int, int, int>(a);
}
// initializer_list constructor tests
{
any a(std::in_place_type<Tracked>);
assert(Tracked::count == 1);
auto &v = a.emplace<Type>({-1, 42, -1});
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(&v == std::any_cast<Type>(&a));
assert(Tracked::count == 0);
assertArgsMatch<Type, std::initializer_list<int>>(a);
}
{
int x = 42;
any a(std::in_place_type<Tracked>);
assert(Tracked::count == 1);
auto &v = a.emplace<Type>({-1, 42, -1}, x);
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(&v == std::any_cast<Type>(&a));
assert(Tracked::count == 0);
assertArgsMatch<Type, std::initializer_list<int>, int&>(a);
}
}
#ifndef TEST_HAS_NO_EXCEPTIONS
struct SmallThrows {
SmallThrows(int) { throw 42; }
SmallThrows(std::initializer_list<int>, int) { throw 42; }
};
static_assert(IsSmallObject<SmallThrows>::value, "");
struct LargeThrows {
LargeThrows(int) { throw 42; }
LargeThrows(std::initializer_list<int>, int) { throw 42; }
int data[sizeof(std::any)];
};
static_assert(!IsSmallObject<LargeThrows>::value, "");
template <class Type>
void test_emplace_throws()
{
// any stores small type
{
std::any a(small{42});
assert(small::count == 1);
try {
auto &v = a.emplace<Type>(101);
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(false);
} catch (int const&) {
}
assert(small::count == 0);
}
{
std::any a(small{42});
assert(small::count == 1);
try {
auto &v = a.emplace<Type>({1, 2, 3}, 101);
static_assert( std::is_same_v<Type&, decltype(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<Type>(101);
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(false);
} catch (int const&) {
}
assert(large::count == 0);
}
{
std::any a(large{42});
assert(large::count == 1);
try {
auto &v = a.emplace<Type>({1, 2, 3}, 101);
static_assert( std::is_same_v<Type&, decltype(v)>, "" );
assert(false);
} catch (int const&) {
}
assert(large::count == 0);
}
}
#endif
template <class T, class ...Args>
constexpr auto has_emplace(int)
-> decltype(std::any{}.emplace<T>(std::declval<Args>()...), true) { return true; }
template <class ...Args>
constexpr bool has_emplace(long) { return false; }
template <class ...Args>
constexpr bool has_emplace() { return has_emplace<Args...>(0); }
template <class T, class IT, class ...Args>
constexpr auto has_emplace_init_list(int)
-> decltype(std::any{}.emplace<T>(
{std::declval<IT>(), std::declval<IT>(), std::declval<IT>()},
std::declval<Args>()...), true) { return true; }
template <class ...Args>
constexpr bool has_emplace_init_list(long) { return false; }
template <class ...Args>
constexpr bool has_emplace_init_list() { return has_emplace_init_list<Args...>(0); }
void test_emplace_sfinae_constraints() {
{
static_assert(has_emplace<int>(), "");
static_assert(has_emplace<int, int>(), "");
static_assert(!has_emplace<int, int, int>(), "not constructible");
static_assert(!has_emplace_init_list<int, int>(), "not constructible from il");
}
{
static_assert(has_emplace<small>(), "");
static_assert(has_emplace<large>(), "");
static_assert(!has_emplace<small, void*>(), "");
static_assert(!has_emplace<large, void*>(), "");
static_assert(has_emplace_init_list<small, int>(), "");
static_assert(has_emplace_init_list<large, int>(), "");
static_assert(!has_emplace_init_list<small, void*>(), "");
static_assert(!has_emplace_init_list<large, void*>(), "");
}
{
// 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, int) {}
};
static_assert(!has_emplace<NoCopy>(), "");
static_assert(!has_emplace<NoCopy, int>(), "");
static_assert(!has_emplace_init_list<NoCopy, int, int, int>(), "");
static_assert(!has_emplace<NoCopy&>(), "");
static_assert(!has_emplace<NoCopy&, int>(), "");
static_assert(!has_emplace_init_list<NoCopy&, int, int, int>(), "");
static_assert(!has_emplace<NoCopy&&>(), "");
static_assert(!has_emplace<NoCopy&&, int>(), "");
static_assert(!has_emplace_init_list<NoCopy&&, int, int, int>(), "");
}
}
int main(int, char**) {
test_emplace_type<small>();
test_emplace_type<large>();
test_emplace_type<small_throws_on_copy>();
test_emplace_type<large_throws_on_copy>();
test_emplace_type<throws_on_move>();
test_emplace_type_tracked<small_tracked_t>();
test_emplace_type_tracked<large_tracked_t>();
test_emplace_sfinae_constraints();
#ifndef TEST_HAS_NO_EXCEPTIONS
test_emplace_throws<SmallThrows>();
test_emplace_throws<LargeThrows>();
#endif
return 0;
}
|