Skip to content

Commit ff3ee98

Browse files
committed
specialize tl expected
1 parent a133f8f commit ff3ee98

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

examples/error_code.cpp

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#include <fmt/core.h>
22
#include <system_error>
33
#include "tl-expected.hpp"
4+
#include <fcntl.h>
45
// 今日主题:现代 C++ 中的错误处理
56

67
// int : [INT_MIN, INT_MAX]
78
// optional<int> : [INT_MIN, INT_MAX] | {nullopt}
89
// variant<int, error_code> : [INT_MIN, INT_MAX] | {error_code}
910
// expected<int, error_code> : [INT_MIN, INT_MAX] | {error_code}
1011

12+
template <>
13+
struct tl::bad_expected_access<std::error_code> : std::system_error {
14+
explicit bad_expected_access(std::error_code e) : std::system_error(std::move(e)) {}
15+
};
16+
1117
namespace mybuss {
1218

1319
enum class login_errc {
@@ -90,12 +96,45 @@ tl::expected<int, std::error_code> sqrfloor(int x) {
9096
// }
9197
}
9298

93-
int main() {
94-
auto ret = sqrfloor(3);
95-
if (ret.has_value()) {
96-
fmt::println("结果: {}", ret.value());
97-
} else {
98-
fmt::println("出错: {}", ret.error().message());
99+
tl::expected<int, std::error_code> expectedStdError(int ret) {
100+
if (ret == -1) {
101+
return tl::unexpected{std::error_code(errno, std::generic_category())};
102+
}
103+
return ret;
104+
}
105+
106+
int checkStdError(int ret) {
107+
if (ret == -1) {
108+
throw std::system_error(std::error_code(errno, std::system_category()));
109+
}
110+
return ret;
111+
}
112+
113+
// tl::expected<void, std::error_code> expectedCudaError(int ret) {
114+
// if (ret != 0) {
115+
// return tl::unexpected{std::error_code(ret, cuda_category())};
116+
// }
117+
// return {};
118+
// }
119+
120+
template <class T>
121+
using expected = tl::expected<T, std::error_code>;
122+
123+
struct RAIIFile {
124+
int fd;
125+
126+
tl::expected<size_t, std::error_code> write(std::span<const char> buf) {
127+
return expectedStdError(::write(fd, buf.data(), buf.size()));
99128
}
129+
};
130+
131+
// tl::expected<void *, std::error_code> cppCudaMalloc(size_t size) {
132+
// expectedCudaError(cudaMalloc(p));
133+
// }
134+
135+
int main() {
136+
RAIIFile file{expectedStdError(open("/tmp/test.log", O_WRONLY)).value()};
137+
std::string s = "asasasas";
138+
file.write(s).value();
100139
return 0;
101140
}

examples/tl-expected.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,10 @@ template <class E> class bad_expected_access : public std::exception {
12441244
/// has been destroyed. The initialization state of the contained object is
12451245
/// tracked by the expected object.
12461246
template <class T, class E>
1247-
class expected : private detail::expected_move_assign_base<T, E>,
1248-
private detail::expected_delete_ctor_base<T, E>,
1249-
private detail::expected_delete_assign_base<T, E>,
1250-
private detail::expected_default_ctor_base<T, E> {
1247+
class [[nodiscard]] expected : private detail::expected_move_assign_base<T, E>,
1248+
private detail::expected_delete_ctor_base<T, E>,
1249+
private detail::expected_delete_assign_base<T, E>,
1250+
private detail::expected_default_ctor_base<T, E> {
12511251
static_assert(!std::is_reference<T>::value, "T must not be a reference");
12521252
static_assert(!std::is_same<T, std::remove_cv<in_place_t>::type>::value,
12531253
"T must not be in_place_t");

0 commit comments

Comments
 (0)