std:: runtime_format
|
Definiert im Header
<format>
|
||
|
/*runtime-format-string*/
<
char
>
runtime_format
(
std::
string_view
fmt
)
noexcept
;
|
(1) | (seit C++26) |
|
/*runtime-format-string*/
<
wchar_t
>
runtime_format
(
std::
wstring_view
fmt
)
noexcept
;
|
(2) | (seit C++26) |
Gibt ein Objekt zurück, das einen Laufzeit-Formatstring speichert, der direkt in benutzerorientierten Formatierungsfunktionen verwendet werden kann und implizit in
std::basic_format_string
konvertiert werden kann.
Inhaltsverzeichnis |
Parameter
| fmt | - | ein String-View |
Rückgabewert
Ein Objekt, das die Laufzeit-Formatzeichenkette des nur zur Darstellung bestimmten Typs enthält:
Klassentemplate
runtime-format-string
<CharT>
|
template
<
class
CharT
>
struct /*runtime-format-string*/ ; |
( Nur zur Darstellung* ) | |
Memberobjekte
Das zurückgegebene Objekt enthält ein nur zur Darstellung bestimmtes nicht-statisches Datenmember
str
vom Typ
std::basic_string_view<CharT>
.
Konstruktoren und Zuweisungen
|
/*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) | |
str
mit
s
.
Hinweise
Da der Rückgabetyp von
runtime_format
weder kopierbar noch verschiebbar ist, verhindert der Versuch,
runtime_fmt
als Glvalue zu übergeben, die Konstruktion von
std::basic_format_string
, was zu einem ill-formed Programm führt. Um
std::basic_format_string
mit
runtime_format
zu konstruieren, wird der Rückgabewert von
runtime_format
direkt als Prvalue an
std::basic_format_string
übergeben, wo Copy Elision garantiert ist.
auto runtime_fmt = std::runtime_format("{}"); auto s0 = std::format(runtime_fmt, 1); // Fehler auto s1 = std::format(std::move(runtime_fmt), 1); // immer noch Fehler auto s2 = std::format(std::runtime_format("{}"), 1); // ok
| Feature-Test Makro | Wert | Std | Funktion |
|---|---|---|---|
__cpp_lib_format
|
202311L
|
(C++26) | Laufzeit-Formatstrings |
Beispiel
#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 += "{} "; // erstellt den Formatierungsstring std::print("{} : ", fmt); std::println(std::runtime_format(fmt), "alpha", 'Z', 3.14, "unused"); } }
Ausgabe:
Hello world!
{} : alpha
{} {} : alpha Z
{} {} {} : alpha Z 3.14
Siehe auch
|
(C++20)
|
speichert die formatierte Darstellung der Argumente in einem neuen String
(Funktions-Template) |
|
(C++20)
|
nicht-template Variante von
std::format
mit typeradierten Argumentdarstellungen
(Funktion) |
|
(C++20)
(C++20)
(C++20)
|
Klassentemplate, das zur Erstellungszeit Überprüfungen der Formatzeichenkette zur Compile-Zeit durchführt
(Klassen-Template) |