-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiostream.hpp
More file actions
49 lines (46 loc) · 1.6 KB
/
Copy pathiostream.hpp
File metadata and controls
49 lines (46 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @brief 输入输出的重载
*/
#pragma once
#include <string_view>
#include <ranges>
namespace iostream {
template <typename T>
concept range_like = std::ranges::range<T> && !std::is_convertible_v<T, std::string_view>;
template <typename T>
concept range_tuple = range_like<T> || std::__is_tuple_like<T>::value && !std::ranges::range<T>;
template <typename Istream>
Istream &operator>>(Istream &istream, range_tuple auto &value) {
if constexpr (range_like<decltype(value)>) {
for (auto &e : value) {
istream >> e;
}
} else {
// clang-format off
// 因为我觉得 这样写更清晰一些 clang-format 感觉差点意思
std::apply([&istream](auto &&...args) {
((istream >> args), ...);
}, value);
// clang-format on
}
return istream;
}
template <typename Ostream>
Ostream &operator<<(Ostream &ostream, const range_tuple auto &value) {
if constexpr (range_like<decltype(value)>) {
auto it = value.begin();
for (ostream << *it++; it != value.end(); ++it) {
ostream << ' ' << *it;
}
} else {
// clang-format off
ostream << '(' << (std::apply([&ostream](auto &&...args) {
auto size = sizeof...(args);
((ostream << args << (--size ? ", " : "")), ...);
}, value), ')');
// clang-format on
}
return ostream;
}
} // namespace iostream
using iostream::operator<<, iostream::operator>>;