Skip to content

Commit 806a8c8

Browse files
committed
formatter : 複数のメンバ変数をもつクラスをformatterで文字列化する例を追加
1 parent fe9f6f7 commit 806a8c8

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

reference/format/formatter.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,47 @@ int main()
231231
赤 blue
232232
```
233233

234+
235+
### 複数のメンバ変数を含むクラスの場合
236+
```cpp example
237+
#include <iostream>
238+
#include <format>
239+
240+
struct Point {
241+
float x, y;
242+
};
243+
244+
template<>
245+
struct std::formatter<Point> : std::formatter<std::string> {
246+
auto format(Point p, std::format_context& ctx) const {
247+
return std::formatter<std::string>::format(
248+
std::format("[{}, {}]", p.x, p.y),
249+
ctx);
250+
}
251+
};
252+
253+
int main()
254+
{
255+
std::cout << std::format("{}", Point{1.2f, 3.4f}) << std::endl;
256+
}
257+
```
258+
* std::format_context[link basic_format_context.md]
259+
* std::format[link format.md]
260+
261+
#### 出力
262+
```
263+
[1.2, 3.4]
264+
```
265+
266+
234267
## バージョン
235268
### 言語
236269
- C++20
237270

238271
### 処理系
239-
- [Clang](/implementation.md#clang): ??
272+
- [Clang](/implementation.md#clang): 17 [mark verified]
240273
- [GCC](/implementation.md#gcc): 13 [mark verified]
241-
- [ICC](/implementation.md#icc): ??
242-
- [Visual C++](/implementation.md#visual_cpp): ??
274+
- [Visual C++](/implementation.md#visual_cpp): 2022 Update 2 [mark verified]
243275

244276
## 関連項目
245277
- [`vector<bool>`](/reference/vector/vector.md)

0 commit comments

Comments
 (0)