std::runtime_format

Header: <format>

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.

# Declarations

/*runtime-format-string*/<char> runtime_format( std::string_view fmt ) noexcept;

(since C++26)

/*runtime-format-string*/<wchar_t> runtime_format( std::wstring_view fmt ) noexcept;

(since C++26)

# Parameters

# Return value

An object holding the runtime format string of the exposition-only type:

# Notes

Since the return type of runtime_format is neither copyable nor movable, an attempt of passing runtime_fmt as 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 prvalue where copy elision is guaranteed.

# Example

#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");
    }
}

# See also