Console Library 8.0.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<class T>
const T & get (size_t index) const
 类型安全地获取指定索引处的元素(常量版本)。
template<class T>
T & unsafe_get (size_t index)
 不安全地获取指定索引处的元素(非常量版本)。
template<class T>
const T & unsafe_get (size_t index) const
 不安全地获取指定索引处的元素(常量版本)。
template<class... Args>
void unpack (Args &...args) const
 将 Box 中的元素按顺序解包到多个变量中(类型安全)。
template<class... Args>
void unsafe_unpack (Args &...args) const
 将 Box 中的元素按顺序解包到多个变量中(不安全版本)。

Friends

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

Detailed Description

存储多个 Item 的异构容器。

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

Constructor & Destructor Documentation

◆ Box()

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

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

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

Member Function Documentation

◆ get() [1/2]

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

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

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

◆ get() [2/2]

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

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

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

◆ unpack()

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

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

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

◆ unsafe_get() [1/2]

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

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

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

◆ unsafe_get() [2/2]

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

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

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

◆ unsafe_unpack()

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

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

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

◆ operator<< [1/2]

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

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

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

◆ operator<< [2/2]

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

将 Box 输出到宽字符流。

Parameters
os宽字符输出流。
box要输出的 Box。
Returns
std::wostream& 返回 os。

The documentation for this class was generated from the following file:
  • include/container/box.h