Namespaces
Variants

std:: format_kind

From cppreference.net
Definido en el encabezado <format>
template < class R >
constexpr /* unspecified */ format_kind = /* unspecified */ ;
(1) (desde C++23)
template < ranges:: input_range R >

requires std:: same_as < R, std:: remove_cvref_t < R >>

constexpr range_format format_kind < R > = /* ver descripción */ ;
(2) (desde C++23)

La plantilla de variable format_kind selecciona un std::range_format apropiado para un rango R .

std :: format_kind < R > se define de la siguiente manera:

Si U es una especialización de std::pair o U es una especialización de std::tuple y std:: tuple_size_v < U > == 2 , std :: format_kind < R > es std :: range_format :: map .
  • En caso contrario, std :: format_kind < R > es std :: range_format :: set .
  • En caso contrario, std :: format_kind < R > es std :: range_format :: sequence .

Un programa que instancia la plantilla principal de la format_kind variable template está mal formado.

Dado un tipo definido por el programa sin calificación cv program-defined type T que modela input_range , un programa puede especializar format_kind para T . Tales especializaciones son utilizables en expresiones constantes, y tienen tipo const std:: range_format .

Implementación posible

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, "instantiating a primary template is not allowed");
    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;
}();

Ejemplo

#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);
// mal formado:
// static_assert(std::format_kind<A> == std::range_format::disabled);
int main() {}

Véase también

especifica cómo debe formatearse un rango
(enum)