20 General utilities library [utilities]
namespace std {
template<class Context>
class basic_format_arg {
public:
class handle;
private:
using char_type = typename Context::char_type;
variant<monostate, bool, char_type,
int, unsigned int, long long int, unsigned long long int,
float, double, long double,
const char_type*, basic_string_view<char_type>,
const void*, handle> value;
template<class T> explicit basic_format_arg(const T& v) noexcept;
explicit basic_format_arg(float n) noexcept;
explicit basic_format_arg(double n) noexcept;
explicit basic_format_arg(long double n) noexcept;
explicit basic_format_arg(const char_type* s);
template<class traits>
explicit basic_format_arg(
basic_string_view<char_type, traits> s) noexcept;
template<class traits, class Allocator>
explicit basic_format_arg(
const basic_string<char_type, traits, Allocator>& s) noexcept;
explicit basic_format_arg(nullptr_t) noexcept;
template<class T>
explicit basic_format_arg(const T* p) noexcept;
public:
basic_format_arg() noexcept;
explicit operator bool() const noexcept;
};
}
An instance of
basic_format_arg provides access to
a formatting argument for user-defined formatters
.The behavior of a program that adds specializations of
basic_format_arg is undefined
.Postconditions:
!(*this). template<class T> explicit basic_format_arg(const T& v) noexcept;
Constraints: The template specialization
typename Context::template formatter_type<T>
meets the
Formatter requirements (
[formatter.requirements])
. The extent to which an implementation determines that
the specialization meets the
Formatter requirements
is unspecified,
except that as a minimum the expression
typename Context::template formatter_type<T>()
.format(declval<const T&>(), declval<Context&>())
shall be well-formed when treated as an unevaluated operand
.Effects:
- if T is bool or char_type,
initializes value with v;
- otherwise, if T is char and char_type is
wchar_t, initializes value with
static_cast<wchar_t>(v);
- otherwise, if T is a signed integer type ([basic.fundamental])
and sizeof(T) <= sizeof(int),
initializes value with static_cast<int>(v);
- otherwise, if T is an unsigned integer type and
sizeof(T) <= sizeof(unsigned int), initializes
value with static_cast<unsigned int>(v);
- otherwise, if T is a signed integer type and
sizeof(T) <= sizeof(long long int), initializes
value with static_cast<long long int>(v);
- otherwise, if T is an unsigned integer type and
sizeof(T) <= sizeof(unsigned long long int), initializes
value with
static_cast<unsigned long long int>(v);
- otherwise, initializes value with handle(v).
explicit basic_format_arg(float n) noexcept;
explicit basic_format_arg(double n) noexcept;
explicit basic_format_arg(long double n) noexcept;
Effects: Initializes
value with
n. explicit basic_format_arg(const char_type* s);
Effects: Initializes
value with
s. template<class traits>
explicit basic_format_arg(basic_string_view<char_type, traits> s) noexcept;
Effects: Initializes
value with
s. template<class traits, class Allocator>
explicit basic_format_arg(
const basic_string<char_type, traits, Allocator>& s) noexcept;
Effects: Initializes
value with
basic_string_view<char_type>(s.data(), s.size()). explicit basic_format_arg(nullptr_t) noexcept;
Effects: Initializes
value with
static_cast<const void*>(nullptr). template<class T> explicit basic_format_arg(const T* p) noexcept;
Constraints:
is_void_v<T> is
true. Effects: Initializes
value with
p. [
Note 1:
Constructing
basic_format_arg from
a pointer to a member is ill-formed unless
the user provides an enabled specialization of
formatter for that pointer to member type
. —
end note]
Returns:
!holds_alternative<monostate>(value). The class
handle allows formatting an object of a user-defined type
.namespace std {
template<class Context>
class basic_format_arg<Context>::handle {
const void* ptr_;
void (*format_)(basic_format_parse_context<char_type>&,
Context&, const void*);
template<class T> explicit handle(const T& val) noexcept;
friend class basic_format_arg<Context>;
public:
void format(basic_format_parse_context<char_type>&, Context& ctx) const;
};
}
Effects: Initializes
ptr_ with addressof(val) and
format_ with
[](basic_format_parse_context<char_type>& parse_ctx,
Context& format_ctx, const void* ptr) {
typename Context::template formatter_type<T> f;
parse_ctx.advance_to(f.parse(parse_ctx));
format_ctx.advance_to(f.format(*static_cast<const T*>(ptr), format_ctx));
}
Effects: Equivalent to: format_(parse_ctx, format_ctx, ptr_);
Effects: Equivalent to: return visit(forward<Visitor>(vis), arg.value);