std:: format_kind
|
定义于头文件
<format>
|
||
|
template
<
class
R
>
constexpr /* 未指定 */ format_kind = /* 未指定 */ ; |
(1) | (C++23 起) |
|
template
<
ranges::
input_range
R
>
要求
std::
same_as
<
R,
std::
remove_cvref_t
<
R
>>
|
(2) | (C++23 起) |
变量模板
format_kind
为范围
R
选择合适的
std::range_format
格式。
std :: format_kind < R > 的定义如下:
- 若 std:: same_as < std:: remove_cvref_t < ranges:: range_reference_t < R >> , R > 为 true ,则 std :: format_kind < R > 为 std :: range_format :: disabled 。
-
否则,若
R::key_type有效且表示一个类型:-
若
R::mapped_type有效且表示一个类型,令U为 std:: remove_cvref_t < ranges:: range_reference_t < R >> 。
-
若
-
-
若
U是 std::pair 的特化,或U是 std::tuple 的特化且满足 std:: tuple_size_v < U > == 2 ,则 std :: format_kind < R > 为 std :: range_format :: map 。
- 否则, std :: format_kind < R > 为 std :: range_format :: set 。
-
若
- 否则, std :: format_kind < R > 为 std :: range_format :: sequence 。
实例化
format_kind
变量模板主模板的程序是非法的。
给定一个符合
input_range
模型的非cv限定
程序定义类型
T
,程序可以为
T
特化
format_kind
。此类特化可在常量表达式中使用,且具有类型
const
std::
range_format
。
可能的实现
namespace detail { template< typename > constexpr bool is_pair_or_tuple_2 = false; template< typename T, typename U > constexpr bool is_pair_or_tuple_2<std::pair<T, U>> = true; template< typename T, typename U > constexpr bool is_pair_or_tuple_2<std::tuple<T, U>> = true; template < typename T > requires std::is_reference_v<T> || std::is_const_v<T> constexpr bool is_pair_or_tuple_2<T> = is_pair_or_tuple_2<std::remove_cvref_t<T>>; } template< class R > constexpr range_format format_kind = [] { static_assert(false, "实例化主模板不被允许"); return range_format::disabled; }(); template< ranges::input_range R > requires std::same_as<R, std::remove_cvref_t<R>> constexpr range_format format_kind<R> = [] { if constexpr (std::same_as<std::remove_cvref_t<std::ranges::range_reference_t<R>>, R>) return range_format::disabled; else if constexpr (requires { typename R::key_type; }) { if constexpr (requires { typename R::mapped_type; } && detail::is_pair_or_tuple_2<std::ranges::range_reference_t<R>>) return range_format::map; else return range_format::set; } else return range_format::sequence; }(); |
示例
#include <filesystem> #include <format> #include <map> #include <set> #include <vector> struct A {}; static_assert(std::format_kind<std::vector<int>> == std::range_format::sequence); static_assert(std::format_kind<std::map<int, int>> == std::range_format::map); static_assert(std::format_kind<std::set<int>> == std::range_format::set); static_assert(std::format_kind<std::filesystem::path> == std::range_format::disabled); // 格式错误: // static_assert(std::format_kind<A> == std::range_format::disabled); int main() {}
参见
|
(C++23)
|
指定范围的格式化方式
(枚举) |