44 const char* what()
const noexcept override
46 return "bad_any_cast";
78 Any() noexcept = default;
95 template<
typename ValueType,
96 typename D =
typename std::decay<ValueType>::type,
97 typename std::enable_if<!std::is_same<D, Any>::value &&
98 std::is_copy_constructible<D>::value,
int>::type = 0>
99 Any(ValueType&& value)
101 emplace<D>(std::forward<ValueType>(value));
131 template<
typename ValueType,
132 typename D =
typename std::decay<ValueType>::type,
133 typename std::enable_if<!std::is_same<D, Any>::value &&
134 std::is_copy_constructible<D>::value,
int>::type = 0>
137 emplace<D>(std::forward<ValueType>(value));
146 bool has_value() const noexcept {
return data_ !=
nullptr; }
149 const std::type_info&
type() const noexcept
151 return data_ ? *ops_->type :
typeid(void);
163 ops_->destroy(data_);
164 ::operator
delete(data_);
171 template<
typename T,
typename... Args>
172 typename std::enable_if<!std::is_reference<T>::value,
T&>::type
176 void* storage = ::operator
new(
sizeof(
T));
179 ::new (storage)
T(std::forward<Args>(args)...);
183 ::operator
delete(storage);
188 return *
static_cast<T*
>(data_);
194 std::swap(ops_, other.ops_);
195 std::swap(data_, other.data_);
202 void (*destroy)(
void*);
203 void (*copyConstruct)(
void* dst,
const void* src);
204 const std::type_info* type;
209 static const TypeOps& opsFor()
211 static const TypeOps ops = {
212 [](
void* p) {
static_cast<T*
>(p)->~T(); },
213 [](
void* dst,
const void* src) { ::new (dst)
T(*
static_cast<const T*
>(src)); },
220 void copyFrom(
const Any& other)
224 void* storage = ::operator
new(other.ops_->size);
227 other.ops_->copyConstruct(storage, other.data_);
231 ::operator
delete(storage);
238 void stealFrom(Any& other)
noexcept
242 other.ops_ =
nullptr;
243 other.data_ =
nullptr;
247 const TypeOps* ops_{
nullptr};
248 void* data_{
nullptr};
251 template<
typename T>
friend const T*
any_cast(
const Any*)
noexcept;
252 template<
typename T>
friend T*
any_cast(Any*)
noexcept;
253 template<
typename T>
friend T any_cast(
const Any&);
254 template<
typename T>
friend T any_cast(Any&);
255 template<
typename T>
friend T any_cast(Any&&);
267 using U =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
268 if (operand && operand->type() ==
typeid(U))
269 return static_cast<const T*
>(operand->data_);
277 using U =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
278 if (operand && operand->type() ==
typeid(U))
279 return static_cast<T*
>(operand->data_);
287 using U =
typename std::decay<T>::type;
288 const U* p = any_cast<U>(&operand);
298 using U =
typename std::decay<T>::type;
299 U* p = any_cast<U>(&operand);
309 using U =
typename std::decay<T>::type;
310 U* p = any_cast<U>(&operand);
313 return static_cast<U&&
>(*p);
322template<
typename T,
typename... Args>
326 a.
emplace<
T>(std::forward<Args>(args)...);
类型擦除的值容器,与 C++17 的 std::any 接口兼容
定义 Any.hpp:71
void swap(Any &other) noexcept
与之交换内容(常量时间,noexcept)
定义 Any.hpp:192
std::enable_if<!std::is_reference< T >::value, T & >::type emplace(Args &&... args)
以给定参数就地构造 T 并替换当前值;返回新值的引用
定义 Any.hpp:173
Any & operator=(Any &&other) noexcept
移动赋值:接管 other 的内容,other 变为空
定义 Any.hpp:120
const std::type_info & type() const noexcept
当前持有值的类型信息;空时返回 typeid(void)
定义 Any.hpp:149
Any & operator=(const Any &other)
拷贝赋值(copy-and-swap,强异常安全)
定义 Any.hpp:109
Any(ValueType &&value)
从任意可拷贝构造类型构造(类型被 decay)
定义 Any.hpp:99
Any(Any &&other) noexcept
移动构造:接管 other 的内容,other 变为空
定义 Any.hpp:87
Any() noexcept=default
默认构造:空
bool has_value() const noexcept
是否持有任一值
定义 Any.hpp:146
Any & operator=(ValueType &&value)
从任意可拷贝构造类型赋值(类型被 decay)
定义 Any.hpp:135
void reset() noexcept
丢弃当前值并置为空
定义 Any.hpp:159
any_cast 失败时抛出的异常
定义 Any.hpp:42
const T * any_cast(const Any *operand) noexcept
探测式取回(const):类型匹配时返回指针,否则 nullptr
定义 Any.hpp:265
Any make_any(Args &&... args)
生成一个持有 T 的 Any
定义 Any.hpp:323