std::runtime_format
| Definiert in 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-Formatierungsstring speichert, der direkt in benutzerorientierten Formatierungsfunktionen verwendbar ist und implizit in std::basic_format_string konvertiert werden kann.
Inhalt |
[bearbeiten] Parameter
| fmt | - | Ein String-View |
[bearbeiten] Rückgabewert
Ein Objekt, das den Laufzeit-Formatierungsstring vom Exposition-Only-Typ hält
Klassenvorlage runtime-format-string <CharT>
| template< class CharT > struct /*runtime-format-string*/; |
(nur Exposition*) | |
Member-Objekte
Das zurückgegebene Objekt enthält einen Exposition-Only-Nicht-statischen 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.[bearbeiten] Hinweise
Da der Rückgabetyp von runtime_format weder kopierbar noch verschiebbar ist, unterdrückt ein 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 zurückgegebene Wert von runtime_format direkt an std::basic_format_string als Prvalue übergeben, wobei Copy Elision garantiert ist.
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-Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_format |
202311L |
(C++26) | Laufzeit-Formatierungsstrings |
[bearbeiten] 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 += "{} "; // constructs the formatting string std::print("{} : ", fmt); std::println(std::runtime_format(fmt), "alpha", 'Z', 3.14, "unused"); } }
Ausgabe
Hello world!
{} : alpha
{} {} : alpha Z
{} {} {} : alpha Z 3.14[bearbeiten] Siehe auch
| (C++20) |
speichert die formatierte Darstellung der Argumente in einem neuen String (Funktionstemplate) |
| (C++20) |
nicht-Template-Variante von std::format mit typ-erased Argumentdarstellung (Funktion) |
| (C++20)(C++20)(C++20) |
Klassentemplate, das zur Konstruktionszeit Format-String-Prüfungen zur Kompilierzeit durchführt (Klassentemplate) |