Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::Box Class Reference

存储多个 Item 的异构容器。 More...

#include <box.h>

Inheritance diagram for console::Box:

Public Member Functions

template<class... Args>
 Box (Args &&...args)
 从任意数量、任意类型的值构造 Box。
template<class T>
T & get (size_t index)
 类型安全地获取指定索引处的元素(非常量版本)。
template<typename T>
T & unsafe_get (size_t index)
 不安全地获取指定索引处的元素(非常量版本)。
template<class... Args>
void unpack (Args &...args)
 Box 中的元素按顺序解包到多个变量中(类型安全)。
template<class... Args>
void unsafe_unpack (Args &...args)
 Box 中的元素按顺序解包到多个变量中(不安全版本)。

Friends

std::ostream & operator<< (std::ostream &os, const Box &box)
 Box 输出到流,格式类似于元组 (elem1, elem2, ...)。

Detailed Description

存储多个 Item 的异构容器。

公有继承自 std::vector<Item>,支持从多个任意类型的值直接构造, 并提供类型安全的元素访问和解包功能。

Constructor & Destructor Documentation

◆ Box()

template<class... Args>
console::Box::Box ( Args &&... args)
inline

从任意数量、任意类型的值构造 Box。

Template Parameters
Args参数包类型。
Parameters
args要存储的值(每个值都会被隐式转换为 Item)。

Member Function Documentation

◆ get()

template<class T>
T & console::Box::get ( size_t index)
inline

类型安全地获取指定索引处的元素(非常量版本)。

Template Parameters
T期望的类型。
Parameters
index元素索引(从 0 开始)。
Returns
T& 元素的引用。
Exceptions
console::bad_get若索引越界或实际类型与 T 不匹配。

◆ unpack()

template<class... Args>
void console::Box::unpack ( Args &... args)
inline

Box 中的元素按顺序解包到多个变量中(类型安全)。

Template Parameters
Args变量包类型。
Parameters
args要赋值的变量(按引用传递)。
Note
若元素数量或类型不匹配会抛出 bad_get 异常。

◆ unsafe_get()

template<typename T>
T & console::Box::unsafe_get ( size_t index)
inline

不安全地获取指定索引处的元素(非常量版本)。

Template Parameters
T期望的类型(必须与实际类型匹配,否则 UB)。
Parameters
index元素索引(从 0 开始)。
Returns
T& 元素的引用。
Warning
不检查索引越界和类型匹配,可能导致未定义行为。

◆ unsafe_unpack()

template<class... Args>
void console::Box::unsafe_unpack ( Args &... args)
inline

Box 中的元素按顺序解包到多个变量中(不安全版本)。

Template Parameters
Args变量包类型。
Parameters
args要赋值的变量(按引用传递)。
Warning
不进行任何类型或边界检查,需确保 Box 大小和类型与参数包完全匹配。

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Box & box )
friend

Box 输出到流,格式类似于元组 (elem1, elem2, ...)。

Parameters
os输出流。
box要输出的 Box。
Returns
std::ostream& 返回 os。

The documentation for this class was generated from the following file: