Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/format/basic format args"

From cppreference.com
< cpp‎ | utility‎ | format
(Member functions: fix)
(LWG4106)
 
(12 intermediate revisions by 7 users not shown)
Line 3: Line 3:
  
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 | since=c++20 |1=
+
{{dcl header|format}}
template<class Context>
+
{{dcl|num=1|since=c++20|1=
 +
template< class Context >
 
class basic_format_args;
 
class basic_format_args;
 
}}
 
}}
{{dcl | num=2 | since=c++20 |1=
+
{{dcl|num=2|since=c++20|1=
 
using format_args = basic_format_args<std::format_context>;
 
using format_args = basic_format_args<std::format_context>;
 
}}
 
}}
{{dcl | num=3 | since=c++20 |1=
+
{{dcl|num=3|since=c++20|1=
 
using wformat_args = basic_format_args<std::wformat_context>;
 
using wformat_args = basic_format_args<std::wformat_context>;
}}
 
{{dcl | num=4 | since=c++20 |1=
 
template<class OutputIt, class CharT>
 
using format_args_t = basic_format_args<std::basic_format_context<OutputIt, CharT>>;
 
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 23: Line 20:
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem ctor | nolink=true | constructs a {{tt|basic_format_args}} object}}
+
{{dsc mem ctor|nolink=true|constructs a {{tt|basic_format_args}} object}}
{{dsc mem fun | get | nolink=true | returns formatting argument at the given index}}
+
{{dsc mem fun|get|nolink=true|returns formatting argument at the given index}}
 
{{dsc end}}
 
{{dsc end}}
  
{{member | {{small|std::basic_format_args::}}basic_format_args |2=
+
{{member|{{small|std::basic_format_args::}}basic_format_args|2=
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 |1=
+
{{dcl|1=
basic_format_args() noexcept;
+
template< class... Args >
}}
+
basic_format_args( const /*format-arg-store*/<Context, Args...>& store ) noexcept;
{{dcl | num=2 |1=
+
template<class... Args>
+
basic_format_args(const /*format-arg-store*/<Context, Args...>& store) noexcept;
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ Constructs a {{tt|basic_format_args}} object that does not hold any formatting argument.
+
Constructs a {{tt|basic_format_args}} object from the result of a call to {{lc|std::make_format_args}} or {{lc|std::make_wformat_args}}.
@2@ Constructs a {{tt|basic_format_args}} object from the result of a call to {{lc|std::make_format_args}} or {{lc|std::make_wformat_args}}.
+
<!---->
+
{{tt|std::basic_format_args}} has reference semantics. It is the programmer's responsibility to ensure that an {{tt|std::basic_format_args}} object does not outlive {{tt|store}} (which, in turn, should not outlive the arguments to {{lc|std::make_format_args}} or {{lc|std::make_wformat_args}}).
+
 
}}
 
}}
{{member | {{small|std::basic_format_args::}}get |2=
+
{{member|{{small|std::basic_format_args::}}get|2=
{{ddcl | 1=
+
{{ddcl|1=
std::basic_format_arg<Context> get(std::size_t i) const noexcept;
+
std::basic_format_arg<Context> get( std::size_t i ) const noexcept;
 
}}
 
}}
Returns a {{lc|std::basic_­format_­arg}} holding the {{tt|i}}-th argument in {{tt|args}}, where {{tt|args}} is the parameter pack passed to {{lc|std::make_format_args}} or {{lc|std::make_wformat_args}}.
+
Returns a {{lc|std::basic_format_arg}} holding the {{c|i}}-th argument in {{tt|args}}, where {{tt|args}} is the parameter pack passed to {{lc|std::make_format_args}} or {{lc|std::make_wformat_args}}.
  
If there's no such formatting argument (i.e. {{tt|*this}} was default-constructed or {{tt|i}} is not less than the number of formatting arguments), returns a default-constructed {{lc|std::basic_­format_­arg}} (holding a {{lc|std::monostate}} object).
+
If there's no such formatting argument (i.e. {{c|*this}} was default-constructed or {{c|i}} is not less than the number of formatting arguments), returns a default-constructed {{lc|std::basic_format_arg}} (holding a {{lc|std::monostate}} object).
 
}}
 
}}
  
===Example===
+
===Deduction guides===
{{example
+
{{ddcl|since=c++20|
|code=
+
template< class Context, class... Args >
|output=
+
basic_format_args( /*format-arg-store*/<Context, Args...> ) -> basic_format_args<Context>;
 
}}
 
}}
 +
 +
===Notes===
 +
{{tt|std::basic_format_args}} has reference semantics. It is the programmer's responsibility to ensure that {{c|*this}} does not outlive {{c|store}} (which, in turn, should not outlive the arguments to {{lc|std::make_format_args}} or {{lc|std::make_wformat_args}}).
 +
 +
===Example===
 +
{{example}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|paper=P2216R3|std=C++20|before={{tt|format_args_t}} was provided due to overparameterization of {{tt|vformat_to}}|after=removed}}
 +
{{dr list item|wg=lwg|dr=3810|std=C++20|before={{tt|basic_format_args}} has no deduction guide|after=added}}
 +
{{dr list item|wg=lwg|dr=4106|std=C++20|before={{tt|basic_format_args}} was default-constructible|after=default constructor removed}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/utility/format/dsc basic_format_arg}}
 +
{{dsc end}}
 +
 +
{{langlinks|es|ja|ru|zh}}

Latest revision as of 00:11, 2 July 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <format>
template< class Context >
class basic_format_args;
(1) (since C++20)
using format_args = basic_format_args<std::format_context>;
(2) (since C++20)
using wformat_args = basic_format_args<std::wformat_context>;
(3) (since C++20)

Provides access to formatting arguments.

Contents

[edit] Member functions

(constructor)
constructs a basic_format_args object
(public member function)
get
returns formatting argument at the given index
(public member function)

std::basic_format_args::basic_format_args

template< class... Args >
basic_format_args( const /*format-arg-store*/<Context, Args...>& store ) noexcept;

Constructs a basic_format_args object from the result of a call to std::make_format_args or std::make_wformat_args.

std::basic_format_args::get

std::basic_format_arg<Context> get( std::size_t i ) const noexcept;

Returns a std::basic_format_arg holding the i-th argument in args, where args is the parameter pack passed to std::make_format_args or std::make_wformat_args.

If there's no such formatting argument (i.e. *this was default-constructed or i is not less than the number of formatting arguments), returns a default-constructed std::basic_format_arg (holding a std::monostate object).

[edit] Deduction guides

template< class Context, class... Args >
basic_format_args( /*format-arg-store*/<Context, Args...> ) -> basic_format_args<Context>;
(since C++20)

[edit] Notes

std::basic_format_args has reference semantics. It is the programmer's responsibility to ensure that *this does not outlive store (which, in turn, should not outlive the arguments to std::make_format_args or std::make_wformat_args).

[edit] Example

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
P2216R3 C++20 format_args_t was provided due to overparameterization of vformat_to removed
LWG 3810 C++20 basic_format_args has no deduction guide added
LWG 4106 C++20 basic_format_args was default-constructible default constructor removed

[edit] See also

class template that provides access to a formatting argument for user-defined formatters
(class template) [edit]