命名空间
变体
操作

std::basic_format_parse_context

来自 cppreference.cn
< cpp‎ | utility‎ | format
 
 
 
格式化库
标准格式规范
格式化函数
(C++20)
(C++20)
(C++20)
(C++20)
格式字符串
格式化概念
Formatter
(C++20)
basic_format_parse_contextformat_parse_contextwformat_parse_context
(C++20)(C++20)(C++20)
格式化参数
(C++20) (在 C++26 中废弃)
格式错误
 
定义于头文件 <format>
template< class CharT >
class basic_format_parse_context;
(C++20 起)

提供对格式字符串解析状态的访问,该状态由正在解析的格式字符串范围和用于自动索引的参数计数器组成。

当解析格式规范时,`std::basic_format_parse_context` 实例会传递给 Formatter

声明 `std::basic_format_parse_context` 的显式或部分特化的程序是病态的,无需诊断。

提供了用于常见字符类型的几个 typedef

定义于头文件 <format>
类型 定义
std::format_parse_context std::basic_format_parse_context<char>
std::wformat_parse_context std::basic_format_parse_context<wchar_t>

目录

[编辑] 成员类型

类型 定义
char_type CharT
iterator std::basic_string_view<CharT>::const_iterator
const_iterator std::basic_string_view<CharT>::const_iterator

[编辑] 成员函数

(构造函数)
从格式字符串和参数计数构造 `std::basic_format_parse_context` 实例
(公开成员函数)
operator=
[已删除]
std::basic_format_parse_context 不可复制
(公开成员函数)
begin
返回格式字符串范围的起始迭代器
(公开成员函数)
end
返回格式字符串范围的结束迭代器
(公开成员函数)
advance_to
将起始迭代器推进到给定位置
(公开成员函数)
next_arg_id
进入自动索引模式,并返回下一个参数索引
(公开成员函数)
check_arg_id
进入手动索引模式,检查给定参数索引是否在范围内
(公开成员函数)
check_dynamic_spec
(C++26)
检查具有给定参数索引的对应格式参数的类型是否在给定类型模板参数中
(公开成员函数)
check_dynamic_spec_integral
(C++26)
检查具有给定参数索引的对应格式参数的类型是否为整型
(公开成员函数)
check_dynamic_spec_string
(C++26)
检查具有给定参数索引的对应格式参数的类型是否为字符串类型
(公开成员函数)

std::basic_format_parse_context::basic_format_parse_context

(1)
constexpr explicit

basic_format_parse_context( std::basic_string_view<CharT> fmt,

                            std::size_t num_args = 0 ) noexcept;
(直到 C++26)
constexpr explicit
basic_format_parse_context( std::basic_string_view<CharT> fmt ) noexcept;
(C++26 起)
basic_format_parse_context( const basic_format_parse_context& ) = delete;
(2)
1) 构造 `std::basic_format_parse_context` 实例。将格式字符串范围初始化为 [fmt.begin()fmt.end()),并将参数计数初始化为 num_args(直到 C++26)0(从 C++26 起)

使用此构造函数初始化的 `std::basic_format_parse_context` 实例的任何 `next_arg_id`、`check_arg_id` 或 `check_dynamic_spec` 调用都不是核心常量表达式。

(C++26 起)
2) 拷贝构造函数已被删除。`std::basic_format_parse_context` 不可拷贝。

std::basic_format_parse_context::begin

constexpr const_iterator begin() const noexcept;

返回格式字符串范围的起始迭代器。

std::basic_format_parse_context::end

constexpr const_iterator end() const noexcept;

返回格式字符串范围的结束迭代器。

std::basic_format_parse_context::advance_to

constexpr void advance_to( const_iterator it );

将格式字符串范围的起始设置为 it。在调用 `advance_to()` 后,后续对 `begin()` 的调用将返回 it 的副本。

如果从 it 无法 到达 end(),则行为未定义。

std::basic_format_parse_context::next_arg_id

constexpr std::size_t next_arg_id();

进入自动参数索引模式,并返回从 0 开始的下一个参数索引。

如果 *this 已经进入手动参数索引模式,则抛出 std::format_error

如果下一个参数索引大于或等于构造函数中提供的 num_args,则该调用不是核心常量表达式。

std::basic_format_parse_context::check_arg_id

constexpr void check_arg_id( std::size_t id );

进入手动参数索引模式。

如果 *this 已经进入自动参数索引模式,则抛出 std::format_error

如果 id 大于或等于构造函数中提供的 num_args,则该调用不是核心常量表达式。

std::basic_format_parse_context::check_dynamic_spec

template< class... Ts >
constexpr void check_dynamic_spec( std::size_t id ) noexcept;
(C++26 起)

如果 id 大于或等于构造函数中提供的 num_args,或者相应的格式参数类型(转换为 std::basic_format_arg 后)不是 Ts... 中的类型之一,则该调用不是核心常量表达式。对 check_dynamic_spec 的调用在运行时没有效果。

除非 sizeof...(Ts) >= 1Ts... 中的类型是唯一的,并且每个类型都是 boolchar_typeintunsigned intlong long intunsigned long long intfloatdoublelong doubleconst char_type*std::basic_string_view<char_type>const void* 之一,否则程序是非良构的。

std::basic_format_parse_context::check_dynamic_spec_integral

constexpr void check_dynamic_spec_integral( std::size_t id ) noexcept;
(C++26 起)

等效于调用 check_dynamic_spec<int, unsigned int, long long int, unsigned long long int>(id)。对 check_dynamic_spec_integral 的调用在运行时没有效果。

std::basic_format_parse_context::check_dynamic_spec_string

constexpr void check_dynamic_spec_string( std::size_t id ) noexcept;
(C++26 起)

等效于调用 check_dynamic_spec<const char_type*, std::basic_string_view<char_type>>(id)。对 check_dynamic_spec_string 的调用在运行时没有效果。

[编辑] 示例

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 3825 C++20 check_arg_id 具有编译时参数
id 检查,但 next_arg_id 没有
已添加
LWG 3975 C++20 允许用户特化 basic_format_parse_context 已禁用