Localization library | |||||||||||||||||||||||||
Regular expressions library(C++11) | |||||||||||||||||||||||||
Formatting library(C++20) | |||||||||||||||||||||||||
Null-terminated sequence utilities | |||||||||||||||||||||||||
Byte strings | |||||||||||||||||||||||||
Multibyte strings | |||||||||||||||||||||||||
Wide strings | |||||||||||||||||||||||||
Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
Text encoding identifications | |||||||||||||||||||||||||
|
Standard format specification | ||||
Formatting functions | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
Format strings | ||||
(C++20)(C++20)(C++20) | ||||
(C++26) | ||||
Formatting concepts | ||||
(C++23) | ||||
Formatter | ||||
(C++20) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++20)(C++20)(C++20) | ||||
(C++20)(C++20)(C++20) | ||||
(C++23) | ||||
(C++23) | ||||
Formatting arguments | ||||
(C++20) | ||||
(C++20) | ||||
(C++20)(C++20)(C++20) | ||||
(C++20)(deprecated in C++26) | ||||
make_format_argsmake_wformat_args (C++20)(C++20) | ||||
Format error | ||||
(C++20) |
Defined in header <format> | ||
template<class Context=std::format_context,class...Args> /*format-arg-store*/<Context, Args...> | (1) | (since C++20) |
template<class...Args> /*format-arg-store*/<std::wformat_context, Args...> | (2) | (since C++20) |
Returns an object that stores an array of formatting arguments and can be implicitly converted tostd::basic_format_args<Context>.
The behavior is undefined iftypename Context::template formatter_type<std::remove_const_t<Ti>> does not meet theBasicFormatter requirements for anyTi
inArgs
.
The program is ill-formed if for any typeTi
inArgs
,Ti
does not satisfy__formattable_with<Context>.
Contents |
args... | - | values to be used as formatting arguments |
An object that holds the formatting arguments.
For each argumentt
of typeT
, letTD
bestd::remove_const_t<std::remove_reference_t<T>>. The correspondingstd::basic_format_arg in the result is determined as below:
TD
isbool orContext::char_type
, thestd::basic_format_arg storest;TD
ischar andContext::char_type
iswchar_t, thestd::basic_format_arg storesstatic_cast<wchar_t>(static_cast<unsignedchar>(t));TD
is a signed integer type whose size is not greater thanint, thestd::basic_format_arg storesstatic_cast<int>(t);TD
is a unsigned integer type whose size is not greater thanunsignedint, thestd::basic_format_arg storesstatic_cast<unsignedint>(t);TD
is a signed integer type whose size is not greater thanlonglong, thestd::basic_format_arg storesstatic_cast<longlong>(t);TD
is a unsigned integer type whose size is not greater thanunsignedlonglong, thestd::basic_format_arg storesstatic_cast<unsignedlonglong>(t);TD
isfloat,double, orlongdouble, thestd::basic_format_arg storest;TD
is astd::basic_string_view orstd::basic_string specialization andTD::char_type
isContext::char_type
, thestd::basic_format_arg storesstd::basic_string_view<Context::char_type>(t.data(), t.size());t
, along with extra data needed forhandle::format()
.A formatting argument has reference semantics for user-defined types and does not extend the lifetime ofargs. It is the programmer's responsibility to ensure thatargs outlive the return value. Usually, the result is only used as argument to formatting function.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_format_uchar | 202311L | (C++20) (DR) | Formatting of code units as unsigned integers |
#include <array>#include <format>#include <iostream>#include <string_view> void raw_write_to_log(std::string_view users_fmt,std::format_args&& args){staticint n{};std::clog<<std::format("{:04} : ", n++)<<std::vformat(users_fmt, args)<<'\n';} template<typename...Args>constexprvoid log(Args&&...args){// Generate formatting string "{} "...std::array<char, sizeof...(Args)*3+1> braces{};constexprconstchar c[4]="{} ";for(auto i{0uz}; i!= braces.size()-1;++i) braces[i]= c[i%3]; braces.back()='\0'; raw_write_to_log(std::string_view{braces.data()}, std::make_format_args(args...));} template<typename T>const T& unmove(T&& x){return x;} int main(){ log("Number","of","arguments","is","arbitrary."); log("Any type that meets the BasicFormatter requirements","can be printed."); log("For example:",1,2.0,'3',"*42*"); raw_write_to_log("{:02} │ {} │ {} │ {}", std::make_format_args(unmove(1), unmove(2.0), unmove('3'),"4"));}
Output:
0000 : Number of arguments is arbitrary.0001 : Any type that meets the BasicFormatter requirements can be printed.0002 : For example: 1 2.0 3 *42*0003 : 01 │ 2.0 │ 3 │ 4
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P2418R2 | C++20 | objects that are neither const-usable nor copyable (such as generator-like objects) are not formattable | allow formatting these objects |
P2905R2 | C++20 | make_format_args accepted rvalue arguments by forwarding references | only takes lvalue references |
P2909R4 | C++20 | char orwchar_t might be formatted as out-of-range unsigned integer values | code units are converted to the corresponding unsigned type before such formatting |
LWG 3631 | C++20 | cv-qualified arguments were incorrectly handled after P2418R2 | handling corrected |
(C++20)(C++20)(C++20) | class that provides access to all formatting arguments (class template)[edit] |
(C++20) | non-template variant ofstd::format using type-erased argument representation (function)[edit] |
(C++20) | non-template variant ofstd::format_to using type-erased argument representation (function template)[edit] |