[format.arg] # 28 Text processing library [[text]](./#text) ## 28.5 Formatting [[format]](format#arg) ### 28.5.8 Arguments [[format.arguments]](format.arguments#format.arg) #### 28.5.8.1 Class template basic_format_arg [format.arg] [🔗](#lib:basic_format_arg) namespace std {templateclass basic_format_arg {public:class handle; private:using char_type = typename Context::char_type; // *exposition only* variant, const void*, handle> value; // *exposition only*template explicit basic_format_arg(T& v) noexcept; // *exposition only*public: basic_format_arg() noexcept; explicit operator bool() const noexcept; templatedecltype(auto) visit(this basic_format_arg arg, Visitor&& vis); template R visit(this basic_format_arg arg, Visitor&& vis); };} [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8454) An instance of basic_format_arg provides access to a formatting argument for user-defined formatters[.](#1.sentence-1) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8458) The behavior of a program that adds specializations ofbasic_format_arg is undefined[.](#2.sentence-1) [🔗](#lib:basic_format_arg,constructor) `basic_format_arg() noexcept; ` [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8468) *Postconditions*: !(*this)[.](#3.sentence-1) [🔗](#itemdecl:2) `template explicit basic_format_arg(T& v) noexcept; ` [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8478) *Constraints*: T satisfies [*formattable-with*](format.formattable#concept:formattable-with "28.5.6.3 Concept formattable [format.formattable]")[.](#4.sentence-1) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8482) *Preconditions*: If decay_t is char_type* or const char_type*,static_cast(v) points to an NTCTS ([[defns.ntcts]](defns.ntcts "3.36 NTCTS"))[.](#5.sentence-1) [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8487) *Effects*: Let TD be remove_const_t[.](#6.sentence-1) - [(6.1)](#6.1) If TD is bool or char_type, initializes value with v; - [(6.2)](#6.2) otherwise, if TD is char and char_type iswchar_t, initializes value withstatic_cast(static_cast(v)); - [(6.3)](#6.3) otherwise, if TD is a signed integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types")) and sizeof(TD) <= sizeof(int), initializes value with static_cast(v); - [(6.4)](#6.4) otherwise, if TD is an unsigned integer type andsizeof(TD) <= sizeof(unsigned int), initializesvalue with static_cast(v); - [(6.5)](#6.5) otherwise, if TD is a signed integer type andsizeof(TD) <= sizeof(long long int), initializesvalue with static_cast(v); - [(6.6)](#6.6) otherwise, if TD is an unsigned integer type andsizeof(TD) <= sizeof(unsigned long long int), initializesvalue withstatic_cast(v); - [(6.7)](#6.7) otherwise, if TD is a standard floating-point type, initializes value with v; - [(6.8)](#6.8) otherwise, if TD is a specialization of basic_string_view or basic_string andTD​::​value_type is char_type, initializes value withbasic_string_view(v.data(), v.size()); - [(6.9)](#6.9) otherwise, if decay_t ischar_type* or const char_type*, initializes value with static_cast(v); - [(6.10)](#6.10) otherwise, if is_void_v> is true oris_null_pointer_v is true, initializes value with static_cast(v); - [(6.11)](#6.11) otherwise, initializes value with handle(v). [*Note [1](#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[.](#6.sentence-2) — *end note*] [🔗](#lib:operator_bool,basic_format_arg) `explicit operator bool() const noexcept; ` [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8550) *Returns*: !holds_alternative(value)[.](#7.sentence-1) [🔗](#lib:visit,basic_format_arg) `template decltype(auto) visit(this basic_format_arg arg, Visitor&& vis); ` [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8562) *Effects*: Equivalent to: return arg.value.visit(std​::​forward(vis)); [🔗](#lib:visit,basic_format_arg_) `template R visit(this basic_format_arg arg, Visitor&& vis); ` [9](#9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8574) *Effects*: Equivalent to: return arg.value.visit(std​::​forward(vis)); [10](#10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8579) The class handle allows formatting an object of a user-defined type[.](#10.sentence-1) [🔗](#lib:basic_format_arg::handle) namespace std {templateclass basic_format_arg::handle {const void* ptr_; // *exposition only*void (*format_)(basic_format_parse_context&, Context&, const void*); // *exposition only*template explicit handle(T& val) noexcept; // *exposition only*public:void format(basic_format_parse_context&, Context& ctx) const; };} [🔗](#lib:basic_format_arg::handle,constructor) `template explicit handle(T& val) noexcept; ` [11](#11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8606) Let - [(11.1)](#11.1) TD be remove_const_t, - [(11.2)](#11.2) TQ be const TD ifconst TD satisfies [*formattable-with*](format.formattable#concept:formattable-with "28.5.6.3 Concept formattable [format.formattable]") and TD otherwise[.](#11.sentence-1) [12](#12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8617) *Mandates*: TQ satisfies [*formattable-with*](format.formattable#concept:formattable-with "28.5.6.3 Concept formattable [format.formattable]")[.](#12.sentence-1) [13](#13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8621) *Effects*: Initializesptr_ with addressof(val) andformat_ with[](basic_format_parse_context& parse_ctx, Context& format_ctx, const void* ptr) {typename Context::template formatter_type f; parse_ctx.advance_to(f.parse(parse_ctx)); format_ctx.advance_to(f.format(*const_cast(static_cast(ptr)), format_ctx));} [🔗](#lib:format,basic_format_arg::handle) `void format(basic_format_parse_context& parse_ctx, Context& format_ctx) const; ` [14](#14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L8643) *Effects*: Equivalent to: format_(parse_ctx, format_ctx, ptr_);