std::make_f...format_args(3)
πΈ NAME πΈ
okay so this is the C++ Standard Libary manual page for the duo std::make_format_args,std::make_wformat_args β and yes, the NAME repeats itself exactly like that: std::make_format_args,std::make_wformat_args. periodt. π
β¨ Synopsis β¨
Defined in header <format>
template< class Context = std::format_context, class... Args >
/*format-arg-store*/<Context, Args...> (1)
(since C++20)
make_format_args( Args&... args );
template< class... Args >
/*format-arg-store*/<std::wformat_context, Args...> (2)
(since C++20)
make_wformat_args( Args&... args );
So what does it do? It returns an object that stores an array of formatting arguments and can be implicitly converted to std::basic_format_args<Context>. <sparkle>iconic behavior, honestly</sparkle>. β¨
β οΈ The behavior is undefined iftypename Context::template formatter_type<std::remove_const_t<Ti>>does not meet theBasicFormatterrequirements for anyTiinArgs. No cap, that's undefined behavior territory. π
β οΈ The program is ill-formed if for any typeTiinArgs,Tidoes not satisfy__formattable_with<Context>. Not cute, not allowed. π
(2) is equivalent to return std::make_format_args<std::wformat_context>(args...);. π«Ά
π Parameters π
args...β values to be used as formatting arguments π
β¨ Returns β¨
An object that holds the formatting arguments. π¦
For each argument t of type T, let TD be std::remove_const_t<std::remove_reference_t<T>>. The corresponding std::basic_format_arg in the result is determined as below:
- if
TDisboolorContext::char_type, thestd::basic_format_argstorest; - otherwise, if
TDischarandContext::char_typeiswchar_t, thestd::basic_format_argstoresstatic_cast<wchar_t>(static_cast<unsigned char>(t)); - otherwise, if
TDis a signed integer type whose size is not greater thanint, thestd::basic_format_argstoresstatic_cast<int>(t); - otherwise, if
TDis a unsigned integer type whose size is not greater thanunsigned int, thestd::basic_format_argstoresstatic_cast<unsigned int>(t); - otherwise, if
TDis a signed integer type whose size is not greater thanlong long, thestd::basic_format_argstoresstatic_cast<long long>(t); - otherwise, if
TDis a unsigned integer type whose size is not greater thanunsigned long long, thestd::basic_format_argstoresstatic_cast<unsigned long long>(t); - otherwise, if
TDisfloat,double, orlong double, thestd::basic_format_argstorest; - otherwise, if
TDis astd::basic_string_vieworstd::basic_stringspecialization andTD::char_typeisContext::char_type, thestd::basic_format_argstoresstd::basic_string_view<Context::char_type>(t.data(), t.size()); - otherwise, if
std::decay_t<TD>isContext::char_type*orconst Context::char_type*, thestd::basic_format_argstoresstatic_cast<const Context::char_type*>(t); - otherwise, if
std::is_void_v<std::remove_pointer_t<TD>>istrueorstd::is_null_pointer_v<TD>istrue, thestd::basic_format_argstoresstatic_cast<const void*>(t); - otherwise, the
std::basic_format_argstores astd::basic_format_arg<Context>::handletot, along with extra data needed forhandle::format().
π§ Notes π§
A formatting argument has reference semantics for user-defined types and does not extend the lifetime of args. It is the programmer's responsibility to ensure that args outlive the return value. Usually, the result is only used as argument to formatting function. bestie, please keep those args alive β this is not a drill. ππ«Ά
- Feature-test macro:
__cpp_lib_format_ucharβ Value:202311Lβ Std:(C++20)β Feature: Formatting of code units as unsigned integers (DR)
π Example π
// Run this code
#include <array>
#include <format>
#include <iostream>
#include <string_view>
void raw_write_to_log(std::string_view users_fmt, std::format_args&& args)
{
static int n{};
std::clog << std::format("{:04} : ", n++) << std::vformat(users_fmt, args) << '\n';
}
template<typename... Args>
constexpr void log(Args&&... args)
{
// Generate formatting string "{} "...
std::array<char, sizeof...(Args) * 3 + 1> braces{};
constexpr const char 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
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. π«Ά
- P2418R2 β Applied to:
C++20- Behavior as published: objects that are neither const-usable nor copyable (such as generator-like objects) are not formattable
- Correct behavior: allow formatting these objects
- P2905R2 β Applied to:
C++20- Behavior as published:
make_format_argsaccepted rvalue arguments by forwarding references - Correct behavior: only takes lvalue references
- Behavior as published:
- P2909R4 β Applied to:
C++20- Behavior as published:
charorwchar_tmight be formatted as out-of-range unsigned integer values - Correct behavior: code units are converted to the corresponding unsigned type before such formatting
- Behavior as published:
- LWG 3631 β Applied to:
C++20- Behavior as published: cv-qualified arguments were incorrectly handled after P2418R2
- Correct behavior: handling corrected
π See also π
basic_format_args,format_args,wformat_argsβ class that provides access to all formatting arguments(C++20)(class template)(C++20)(C++20)vformatβ non-template variant ofstd::formatusing type-erased argument representation(C++20)(function)vformat_toβ non-template variant ofstd::format_tousing type-erased argument representation(C++20)(function template)
π that's the whole page, bestie. source/version/date sign-off: http://cppreference.com Β· 2024.06.10 Β· page title std::make_f...format_args(3). periodt, go format something cute. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms