Difference between revisions of "cpp/utility/format/runtime format"
From cppreference.com
(added c++26 std::runtime_format) |
m |
||
Line 12: | Line 12: | ||
{{dcl end}} | {{dcl end}} | ||
− | Returns an object that stores a format string and can be implicitly converted to {{lc|std::basic_format_string}}. | + | Returns an object that stores a runtime format string directly usable in user-oriented formatting functions and can be implicitly converted to {{lc|std::basic_format_string}}. |
− | @1 | + | @1,2@ Equivalent to {{c|return {fmt};}}. |
===Parameters=== | ===Parameters=== | ||
Line 22: | Line 22: | ||
===Return value=== | ===Return value=== | ||
− | An object holding the format string | + | An object holding the runtime format string. |
===Example=== | ===Example=== |
Revision as of 00:31, 25 November 2023
Defined in header <format>
|
||
/*runtime-format-string*/<char> runtime_format( std::string_view fmt ); |
(1) | (since C++26) |
/*runtime-format-string*/<wchar_t> runtime_format( std::wstring_view fmt ); |
(2) | (since C++26) |
Returns an object that stores a runtime format string directly usable in user-oriented formatting functions and can be implicitly converted to std::basic_format_string.
1,2) Equivalent to return {fmt};.
Contents |
Parameters
fmt | - |
Each replacement field has the following format:
1) replacement field without a format specification
2) replacement field with a format specification
|
Return value
An object holding the runtime format string.
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
(C++20) |
stores formatted representation of the arguments in a new string (function template) |
(C++20) |
non-template variant of std::format using type-erased argument representation (function) |
(C++20)(C++20)(C++20) |
class template that performs compile-time format string checks at construction time (class template) |