std::runtime_format(3)
πΈ NAME πΈ
std::runtime_format β literally std::runtime_format, bestie. (Page title: std::runtime_format(3).) This is from the C++ Standard Libary manual β yes, thatβs the spelling up top, and weβre keeping it π
.
β¨ Synopsis β¨
Defined in header <format>:
/*runtime-format-string*/<char> runtime_format( std::string_view fmt ) noexcept; (1) (since C++26)
/*runtime-format-string*/<wchar_t> runtime_format( std::wstring_view fmt ) noexcept; (2) (since C++26)
Okay so what it gives you back is an object that stores a runtime format string directly usable in user-oriented formatting functions, and it can be implicitly converted to std::basic_format_string. Itβs giving βformat string but make it runtime.β β¨
π Parameters π
fmtβ a string view. Thatβs the whole parameter, bestie π«Ά
π Return value π
An object holding the runtime format string of the exposition-only type:
π§ Class template runtime-format-string <CharT> π§
template< class CharT > (exposition only*)
struct /*runtime-format-string*/;
π Member objects π
The returned object contains an exposition-only non-static data member str of type std::basic_string_view<CharT>. lowkey important π
π¦ Constructors and assignments π¦
/*runtime-format-string*/( std::basic_string_view<CharT> s ) noexcept; (1)
/*runtime-format-string*/( const /*runtime-format-string*/& ) = delete; (2)
/*runtime-format-string*/& operator=( const /*runtime-format-string*/& ) = delete; (3)
- Initializes
strwiths. - Copy constructor is explicitly deleted. The type is neither copyable nor movable.
- The assignment is explicitly deleted.
π Notes π
Okay so hereβs the chaotic part: since the return type of runtime_format is neither copyable nor movable, an attempt of passing runtime_fmt as a glvalue inhibits the construction of std::basic_format_string, which results in program ill-formed π. To construct std::basic_format_string with runtime_format, the returned value of runtime_format is passed directly on std::basic_format_string as a prvalue where copy elision is guaranteed. No cap, thatβs the rule.
auto runtime_fmt = std::runtime_format("{}");
auto s0 = std::format(runtime_fmt, 1); // error
auto s1 = std::format(std::move(runtime_fmt), 1); // still error
auto s2 = std::format(std::runtime_format("{}"), 1); // ok
π₯ Feature-test macro π₯
__cpp_lib_formatβ value202311L, std(C++26), feature:Runtime format strings.
π Example π
// Run this code
#include <format>
#include <print>
#include <string>
#include <string_view>
int main()
{
std::print("Hello {}!\n", "world");
std::string fmt;
for (int i{}; i != 3; ++i)
{
fmt += "{} "; // constructs the formatting string
std::print("{} : ", fmt);
std::println(std::runtime_format(fmt), "alpha", 'Z', 3.14, "unused");
}
}
π Output π
Hello world!
{} : alpha
{} {} : alpha Z
{} {} {} : alpha Z 3.14
π See also π
formatβ stores formatted representation of the arguments in a new string(C++20)(function template).vformatβ non-template variant ofstd::formatusing type-erased argument representation(C++20)(function).basic_format_string,format_string,wformat_stringβ class template that performs compile-time format string checks at construction time(C++20)(class template).
π sourced from http://cppreference.com Β· version/date 2024.06.10 Β· page std::runtime_format(3) Β· k bye bestie π«Ά
π¦ 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