5#if defined(__cpp_lib_any) || __cplusplus >= 201703l
13 template <
typename value_t>
14 value_t any_cast(
const any& any);
16 template <
typename value_t>
17 value_t* any_cast(any* any)
noexcept;
19 template <
typename value_t>
20 const value_t* any_cast(
const any* any)
noexcept;
22 class bad_any_cast :
public std::bad_cast {
24 const char* what() const noexcept
override {
return "bad any cast";}
29 any() noexcept = default;
30 any(const any& other) : content(other.content ? other.content->clone() :
nullptr) {}
31 template <
typename value_t>
32 any(
const value_t& value) : content(new derived<value_t>(value)) {}
33 template <
typename value_t>
34 any(value_t&& value) : content(new derived<value_t>(std::move(value))) {}
38 any& operator=(
const any& other) {
39 if (
this == &other)
return *
this;
41 content =
other.content ?
other.content->clone() :
nullptr;
45 any& operator=(
const any&& other)
noexcept {
46 if (
this == &other)
return *
this;
48 content =
other.content ? std::move(
other.content) : nullptr;
52 template <
typename value_t>
53 any& operator=(
const value_t& value) {
55 content =
new derived<value_t>(value);
59 bool has_value() const noexcept {
return content !=
nullptr;}
61 const std::type_info&
type() const noexcept {
return content ? content->type :
typeid(void);}
63 void reset() noexcept {
delete content;}
66 template <
typename value_t>
67 friend value_t any_cast(
const any& any);
69 template <
typename value_t>
70 friend value_t* any_cast(any* any)
noexcept;
72 template <
typename value_t>
73 friend const value_t* any_cast(
const any* any)
noexcept;
75 template <
typename value_t>
77 return static_cast<derived<value_t>*
>(content)->data;
80 template <
typename value_t>
81 const value_t& value()
const {
82 return static_cast<derived<value_t>*
>(content)->data;
86 base(
const type_info& type) :
type(
type) {}
88 virtual base* clone()
const = 0;
90 const type_info&
type;
93 template <
typename value_t>
94 struct derived :
public base {
95 derived(
const value_t& value) : base(typeid(value)), data(value) {}
98 value_t& operator*() {
return data;}
100 base* clone()
const override {
101 return new derived<value_t>(*
this);
105 base* content =
nullptr;
108 template <
typename value_t>
109 value_t any_cast(
const any& any) {
110 if (!
any.has_value() ||
typeid(value_t) !=
any.type())
throw bad_any_cast();
111 return any.value<value_t>();
114 template <
typename value_t>
115 value_t* any_cast(any* any)
noexcept {
116 if (any ==
nullptr || !
any->has_value() ||
typeid(value_t) !=
any->type())
return nullptr;
117 return any->value<value_t>();
120 template <
typename value_t>
121 const value_t* any_cast(
const any* any)
noexcept {
122 if (any ==
nullptr || !
any->has_value() ||
typeid(value_t) !=
any->type())
return nullptr;
123 return any->value<value_t>();
126 template <
typename value_t>
127 any make_any(value_t&& value) {
128 return any(std::forward<value_t>(value));
std::type_info type
Stores information about a type.
Definition types.h:219
@ any
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
@ other
The operating system is other.