std::basic_format_parse_context

来自cppreference.com
< cpp‎ | utility‎ | format
 
 
 
 
在标头 <format> 定义
template<class CharT >
class basic_format_parse_context;
(C++20 起)

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

分析格式说明时,会传递一个 basic_format_parse_context 实例给格式化器(Formatter)

声明 std::basic_format_parse_context 的显式或部分特化的程序非良构,无须诊断。

提供了几个针对常用字符类型的 typedef:

在标头 <format> 定义
类型 定义
std::format_parse_contextstd::basic_format_parse_context<char>
std::wformat_parse_contextstd::basic_format_parse_context<wchar_t>

目录

[编辑]成员类型

类型 定义
char_typeCharT
iteratorstd::basic_string_view<CharT>::const_iterator
const_iteratorstd::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)
constexprexplicit

basic_format_parse_context(std::basic_string_view<CharT> fmt,

                            std::size_t num_args =0)noexcept;
(C++26 前)
constexprexplicit
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_idcheck_arg_idcheck_dynamic_spec 的任何调用都不是核心常量表达式。

(C++26 起)
2) 复制构造函数被弃置。std::basic_format_parse_context 不可复制。

std::basic_format_parse_context::begin

constexpr const_iterator begin()constnoexcept;

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

std::basic_format_parse_context::end

constexpr const_iterator end()constnoexcept;

返回指向格式字符串范围末尾的迭代器。

std::basic_format_parse_context::advance_to

constexprvoid advance_to( const_iterator it );

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

如果 end()it可及,那么行为未定义。

std::basic_format_parse_context::next_arg_id

constexprstd::size_t next_arg_id();

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

如果 *this 已进入手动参数索引模式,那么就会抛出 std::format_error

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

std::basic_format_parse_context::check_arg_id

constexprvoid check_arg_id(std::size_t id );

进入手动参数索引模式。

如果 *this 已进入自动参数索引模式,那么就会抛出 std::format_error

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

{{member|std::basic_format_parse_context::check_dynamic_spec|2=

template<class... Ts>
constexprvoid 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_typeintunsignedintlonglongintunsignedlonglongintfloatdoublelongdoubleconst char_type*std::basic_string_view<char_type>constvoid* 之一,否则程序非良构。

std::basic_format_parse_context::check_dynamic_spec_integral

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

等价于调用 check_dynamic_spec<int, unsignedint, longlongint, unsignedlonglongint>(id)。在运行时调用 check_dynamic_spec_integral 没有效果。

std::basic_format_parse_context::check_dynamic_spec_string

constexprvoid 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 3845 C++20 check_arg_id 会在编译器检查 id,但 next_arg_id 不会 也会检查
LWG 3975 C++20 允许用户特化 basic_format_parse_context 已禁止
close