Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/format/runtime format"

From cppreference.com
< cpp‎ | utility‎ | format
(added c++26 std::runtime_format)
 
m (-linebreak)
 
(5 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
{{dcl header|format}}
 
{{dcl header|format}}
 
{{dcl|num=1|since=c++26|1=
 
{{dcl|num=1|since=c++26|1=
/*runtime-format-string*/<char> runtime_format( std::string_view fmt );
+
/*runtime-format-string*/<char> runtime_format( std::string_view fmt ) noexcept;
 
}}
 
}}
 
{{dcl|num=2|since=c++26|1=
 
{{dcl|num=2|since=c++26|1=
/*runtime-format-string*/<wchar_t> runtime_format( std::wstring_view fmt );
+
/*runtime-format-string*/<wchar_t> runtime_format( std::wstring_view fmt ) noexcept;
 
}}
 
}}
 
{{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 {{rlpt|basic_format_string|std::basic_format_string}}.
 
+
@1-2@ Equivalent to {{c|return {fmt};}}.
+
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par|fmt|{{include|cpp/utility/format/format string|const}}}}
+
{{par|fmt|a string view}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
An object holding the format string
+
An object holding the runtime format string of the exposition-only type:
 +
{{member|{{petty|'''Class template'''}} {{small|{{tti|runtime-format-string}} {{tt|<CharT>}}}}|2=
 +
 
 +
{{dcl begin}}
 +
{{dcl|notes={{mark expos}}|1=
 +
template< class CharT >
 +
struct /*runtime-format-string*/;
 +
}}
 +
{{dcl end}}
 +
 
 +
====Member objects====
 +
The returned object contains an exposition-only non-static data member {{tti|str}} of type {{lc|std::basic_string_view<CharT>}}.
 +
 
 +
====Constructors and assignments====
 +
{{dcl begin}}
 +
{{dcl|num=1|1=
 +
/*runtime-format-string*/( std::basic_string_view<CharT> s ) noexcept;
 +
}}
 +
{{dcl|num=2|1=
 +
/*runtime-format-string*/( const /*runtime-format-string*/& ) = delete;
 +
}}
 +
{{dcl|num=3|1=
 +
/*runtime-format-string*/& operator=( const /*runtime-format-string*/& ) = delete;
 +
}}
 +
{{dcl end}}
 +
 
 +
@1@ Initializes {{tti|str}} with {{tt|s}}.
 +
@2@ Copy constructor is explicitly deleted. The type is neither copyable nor movable.
 +
@3@ The assignment is explicitly deleted.
 +
}}
 +
 
 +
===Notes===
 +
Since the return type of {{tt|runtime_format}} is neither copyable nor movable, an attempt of passing {{c|runtime_fmt}} as glvalue inhibits the construction of {{lc|std::basic_format_string}} which results in program ill-formed. To construct {{tt|std::basic_format_string}} with {{tt|runtime_format}}, the returned value of {{tt|runtime_format}} is passed directly on {{tt|std::basic_format_string}} as prvalue where copy elision is guaranteed.
 +
 
 +
{{source|1=
 +
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
 +
}}
 +
 
 +
{{ftm begin}}
 +
{{ftm|std=C++26|value=202311L|__cpp_lib_format|Runtime format strings}}
 +
{{ftm end}}
  
 
===Example===
 
===Example===

Latest revision as of 03:45, 10 July 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
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)

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.

Contents

[edit] Parameters

fmt - a string view

[edit] Return value

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

Class template runtime-format-string <CharT>

template< class CharT >
struct /*runtime-format-string*/;
(exposition only*)

Member objects

The returned object contains an exposition-only non-static data member str of type std::basic_string_view<CharT>.

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)
1) Initializes str with s.
2) Copy constructor is explicitly deleted. The type is neither copyable nor movable.
3) The assignment is explicitly deleted.

[edit] 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.

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 Value Std Feature
__cpp_lib_format 202311L (C++26) Runtime format strings

[edit] 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");
    }
}

Output:

Hello world!
{}  : alpha
{} {}  : alpha Z
{} {} {}  : alpha Z 3.14

[edit] See also

(C++20)
stores formatted representation of the arguments in a new string
(function template) [edit]
(C++20)
non-template variant of std::format using type-erased argument representation
(function) [edit]
class template that performs compile-time format string checks at construction time
(class template) [edit]