From 806a8c82d9cefa9e298a1ab907a7518d2be1bea7 Mon Sep 17 00:00:00 2001 From: Akira Takahashi Date: Wed, 18 Sep 2024 07:26:07 +0900 Subject: [PATCH] =?UTF-8?q?formatter=20:=20=E8=A4=87=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E3=83=A1=E3=83=B3=E3=83=90=E5=A4=89=E6=95=B0=E3=82=92=E3=82=82?= =?UTF-8?q?=E3=81=A4=E3=82=AF=E3=83=A9=E3=82=B9=E3=82=92formatter=E3=81=A7?= =?UTF-8?q?=E6=96=87=E5=AD=97=E5=88=97=E5=8C=96=E3=81=99=E3=82=8B=E4=BE=8B?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reference/format/formatter.md | 38 ++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/reference/format/formatter.md b/reference/format/formatter.md index 1c85b3d09..9db8d6c16 100644 --- a/reference/format/formatter.md +++ b/reference/format/formatter.md @@ -231,15 +231,47 @@ int main() 赤 blue ``` + +### 複数のメンバ変数を含むクラスの場合 +```cpp example +#include +#include + +struct Point { + float x, y; +}; + +template<> +struct std::formatter : std::formatter { + auto format(Point p, std::format_context& ctx) const { + return std::formatter::format( + std::format("[{}, {}]", p.x, p.y), + ctx); + } +}; + +int main() +{ + std::cout << std::format("{}", Point{1.2f, 3.4f}) << std::endl; +} +``` +* std::format_context[link basic_format_context.md] +* std::format[link format.md] + +#### 出力 +``` +[1.2, 3.4] +``` + + ## バージョン ### 言語 - C++20 ### 処理系 -- [Clang](/implementation.md#clang): ?? +- [Clang](/implementation.md#clang): 17 [mark verified] - [GCC](/implementation.md#gcc): 13 [mark verified] -- [ICC](/implementation.md#icc): ?? -- [Visual C++](/implementation.md#visual_cpp): ?? +- [Visual C++](/implementation.md#visual_cpp): 2022 Update 2 [mark verified] ## 関連項目 - [`vector`](/reference/vector/vector.md)