命名空间
变体
操作

std::basic_format_arg

来自 cppreference.cn
< cpp‎ | utility‎ | format
 
 
 
 
定义于头文件 <format>
template< class Context >
class basic_format_arg;
(自 C++20 起)

提供对格式化参数的访问。

basic_format_arg 对象通常由 std::make_format_args 创建,并通过 std::visit_format_argvisit 成员函数(自 C++26 起) 访问。

basic_format_arg 对象的行为就像它存储了以下类型的 std::variant

  • std::monostate (仅当对象是默认构造的时)
  • bool
  • Context::char_type
  • int
  • unsigned int
  • long long int
  • unsigned long long int
  • float
  • double
  • long double
  • const Context::char_type*
  • std::basic_string_view<Context::char_type>
  • const void*
  • basic_format_arg::handle

目录

[编辑] 成员类

(C++20)
类型擦除的包装器,允许格式化用户定义类型的对象
(公有成员类)

[编辑] 成员函数

(构造函数)
(C++20)
构造一个 std::basic_format_arg
(公有成员函数)
operator bool
(C++20)
检查当前对象是否持有格式化参数
(公有成员函数)
visit
(C++26)
访问存储的格式化参数
(公有成员函数)

[编辑] 非成员函数

(C++20) (在 C++26 中已弃用)
用于用户定义格式化器的参数访问接口
(函数模板) [编辑]

std::basic_format_arg::basic_format_arg

basic_format_arg() noexcept;
(自 C++20 起)

默认构造函数。构造一个不持有格式化参数的 basic_format_arg。存储的对象类型为 std::monostate

要创建一个持有格式化参数的 basic_format_arg,必须使用 std::make_format_args

std::basic_format_arg::operator bool

explicit operator bool() const noexcept;
(自 C++20 起)

检查 *this 是否持有格式化参数。

如果 *this 持有格式化参数 (即存储的对象类型不是 std::monostate),则返回 true,否则返回 false

std::basic_format_arg::visit

template< class Visitor >
decltype(auto) visit( this basic_format_arg arg, Visitor&& vis );
(1) (自 C++26 起)
template< class R, class Visitor >
R visit( this basic_format_arg arg, Visitor&& vis );
(2) (自 C++26 起)

将访问器 vis 应用于 arg 中包含的对象。

visit 函数不会修改调用它的 basic_format_arg 对象,因为在调用 vis 时使用了该对象的副本。

1) 等效于 return std::visit(std::forward<Visitor>(vis), v);,其中 v 是存储在 arg 中的 std::variant
2) 等效于 return std::visit<R>(std::forward<Visitor>(vis), v);,其中 v 是存储在 arg 中的 std::variant

[编辑] 注解

特性测试 Std 特性
__cpp_lib_format 202306L (C++26) 成员 visit

[编辑] 示例

[编辑] 参见

提供对所有格式化参数的访问的类
(类模板) [编辑]