57 virtual Base *clone()
const = 0;
58 virtual void print(std::ostream &)
const = 0;
59 virtual std::string
str()
const = 0;
60 virtual const std::type_info &type()
const = 0;
76 Derived(
const T &v) : value(v) {}
82 Derived(
T &&v) : value(std::move(v)) {}
88 Base *clone()
const override
90 return new Derived(value);
97 void print(std::ostream &os)
const override
106 std::string
str()
const override
108 std::ostringstream oss;
117 const std::type_info &type()
const override
134 template <
typename T>
136 : ptr(new Derived<typename std::decay<
T>::type>(
137 std::forward<
T>(value))) {}
144 : ptr(other.ptr ? other.ptr->clone() : nullptr) {}
161 template <
typename T>
166 if (
typeid(
T) != ptr->type())
167 throw bad_get(std::string(
"type mismatch: ") +
171 return ((Derived<T> *)ptr)->value;
180 template <
typename T>
183 return ((Derived<T> *)ptr)->value;
192 template <
typename T>
197 if (
typeid(
T) != ptr->type())
198 throw bad_get(std::string(
"type mismatch: ") +
202 return ((Derived<T> *)ptr)->value;
211 template <
typename T>
214 return ((Derived<T> *)ptr)->value;
226 Base *new_ptr = other.ptr ? other.ptr->clone() :
nullptr;
280 class Box :
public std::vector<Item>
288 template <
class... Args>
290 : std::vector<
Item>({
Item(std::forward<Args>(args))...}) {}
302 return std::vector<Item>::at(index).get<
T>();
312 template <
typename T>
315 return std::vector<Item>::operator[](index).unsafe_get<
T>();
324 template <
class... Args>
328 int _[] = {0, ((args =
get<Args>(i++)), 0)...};
338 template <
class... Args>
356 auto it = box.begin();
358 while (++it != box.end())
void unpack(Args &...args)
将 Box 中的元素按顺序解包到多个变量中(类型安全)。
Definition box.h:325
T & get(size_t index)
类型安全地获取指定索引处的元素(非常量版本)。
Definition box.h:300
Box(Args &&...args)
从任意数量、任意类型的值构造 Box。
Definition box.h:289
friend std::ostream & operator<<(std::ostream &os, const Box &box)
将 Box 输出到流,格式类似于元组 (elem1, elem2, ...)。
Definition box.h:352
T & unsafe_get(size_t index)
不安全地获取指定索引处的元素(非常量版本)。
Definition box.h:313
void unsafe_unpack(Args &...args)
将 Box 中的元素按顺序解包到多个变量中(不安全版本)。
Definition box.h:339
可存储任意类型单个对象的类型擦除包装器。
Definition box.h:51
T & unsafe_get()
不安全地获取存储值的引用(非常量版本)。
Definition box.h:181
Item(Item &&other) noexcept
移动构造函数。
Definition box.h:150
friend std::ostream & operator<<(std::ostream &os, const Item &item)
将 Item 输出到流。
Definition box.h:255
~Item()
析构函数,释放内部堆内存。
Definition box.h:271
Item()
默认构造一个空 Item(ptr 为 nullptr)。
Definition box.h:127
const T & get() const
类型安全地获取存储值的引用(常量版本)。
Definition box.h:193
Item(const Item &other)
拷贝构造函数。
Definition box.h:143
T & get()
类型安全地获取存储值的引用(非常量版本)。
Definition box.h:162
const Item & operator=(const Item &other)
拷贝赋值运算符。
Definition box.h:222
std::string str() const
返回 Item 的字符串表示。
Definition box.h:265
const Item & operator=(Item &&other) noexcept
移动赋值运算符。
Definition box.h:238
const T & unsafe_get() const
不安全地获取存储值的引用(常量版本)。
Definition box.h:212
Item(T &&value)
从任意类型构造 Item(万能引用)。
Definition box.h:135
表示从 Item 或 Box 中获取类型不匹配或空值时发生的错误。
Definition csexc.h:105
定义 console 库使用的自定义异常类层次结构。
enable_if_string< T > repr(T &&value, std::ostream &os=std::cout)
输出字符串类型(std::string, const char* 等)的表示,带双引号。
Definition repr.h:79
std::string tiname(const std::type_info &ti)
获取类型信息的可读名称(跨平台 demangle)。
Definition repr.h:56
提供自定义字面量运算符,包括字符串字面量、时间字面量和格式化字符串字面量。
class console::Output print
全局输出对象,模仿 Python 的 print 函数。