std::dynamic_format - cppreference.com

From cppreference.com

Defined in header

<format>

/*dynamic-format-string*/<char>dynamic_format(std::string_viewfmt)noexcept; (1) (since C++26)/*dynamic-format-string*/<wchar_t>dynamic_format(std::wstring_viewfmt)noexcept; (2) (since C++26)Returns an object that stores a dynamic format string directly usable in user-oriented formatting functions and can be implicitly converted to

std::basic_format_string

.

Parameters

fmt - a string view Return value

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

Class templatedynamic-format-string<CharT>

template<classCharT>struct/*dynamic-format-string*/;(since C++26)
(exposition only*)Data members

Member Description std::basic_string_view<CharT>strholds the format string
(exposition-only member object*)Constructors and assignments

/*dynamic-format-string*/(std::basic_string_view<CharT>s)noexcept; (1) (since C++26)/*dynamic-format-string*/(const/*dynamic-format-string*/&)=delete; (2) (since C++26)/*dynamic-format-string*/&operator=(const/*dynamic-format-string*/&)=delete; (3) (since C++26)The type is neither copyable nor movable.

1) Initializes str with s.

2) Copy constructor is explicitly deleted.

3) The assignment is explicitly deleted.

Notes

Since the return type of std::dynamic_format is neither copyable nor movable, an attempt of passing an object dynamic_fmt (see below) as glvalue inhibits the construction of std::basic_format_string which results in program ill-formed. To construct std::basic_format_string with std::dynamic_format, the returned value of std::dynamic_format is passed directly on std::basic_format_string as prvalue where copy elision is guaranteed.

autodynamic_fmt=std::dynamic_format("{}");autos0=std::format(dynamic_fmt,1);// Error: use of deleted functionautos1=std::format(std::move(dynamic_fmt),1);// still errorautos2=std::format(std::dynamic_format("{}"),1);// OK

Feature-test

macro ValueStdFeature

__cpp_lib_format

202311L

(C++26)Runtime format strings

202603L

(C++26)Renaming runtime_format to dynamic_formatExample

Run this code

#include<format>#include<print>#include<string>#include<string_view>intmain(){std::print("Hello {}!\n","world");std::stringfmt;for(inti{};i!=3;++i){fmt+="{} ";// constructs the formatting stringstd::print("{} : ",fmt);std::println(std::dynamic_format(fmt),"alpha",'Z',3.14,"unused");}}Output:

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