Skip to content

Commit 4dd330b

Browse files
authored
refactor: add noexcept and const specifiers to Result methods
1 parent 38caeb0 commit 4dd330b

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

include/common/result.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Result {
3333
* - T must have a default constructor
3434
*/
3535
constexpr Result()
36+
noexcept(std::is_nothrow_default_constructible_v<T>)
3637
requires std::default_initializable<T>
3738
= default;
3839

@@ -47,6 +48,7 @@ class Result {
4748
* - T must be constructible with perfectly forwarded value argument
4849
*/
4950
template<typename U>
51+
// TODO: figure out correct noexcept specifier
5052
requires std::constructible_from<T, U&&>
5153
constexpr Result(U&& value)
5254
: value(std::forward<U>(value)) {}
@@ -60,6 +62,7 @@ class Result {
6062
* - T must be default initializable
6163
*/
6264
constexpr Result(E error)
65+
noexcept(std::is_nothrow_default_constructible_v<T>)
6366
requires std::default_initializable<T>
6467
: error(error) {}
6568

@@ -75,6 +78,7 @@ class Result {
7578
* - T must be constructible with perfectly forwarded value argument
7679
*/
7780
template<typename U>
81+
// TODO: figure out correct noexcept specifier
7882
requires std::constructible_from<T, U&&>
7983
constexpr Result(E error, U&& value)
8084
: error(error),
@@ -85,7 +89,7 @@ class Result {
8589
*
8690
* @return T&
8791
*/
88-
constexpr operator T&() & {
92+
constexpr operator T&() & noexcept {
8993
return value;
9094
}
9195

@@ -94,7 +98,7 @@ class Result {
9498
*
9599
* @return const T&
96100
*/
97-
constexpr operator const T&() const& {
101+
constexpr operator const T&() const& noexcept {
98102
return value;
99103
}
100104

@@ -103,7 +107,7 @@ class Result {
103107
*
104108
* @return T&&
105109
*/
106-
constexpr operator T&&() && {
110+
constexpr operator T&&() && noexcept {
107111
return std::move(value);
108112
}
109113

@@ -117,6 +121,7 @@ class Result {
117121
* @return "normal" value
118122
*/
119123
template<typename Self>
124+
// TODO: figure out correct noexcept specifier
120125
constexpr auto get_value(this Self&& self) {
121126
return std::forward<Self>(self).value;
122127
}
@@ -127,7 +132,7 @@ class Result {
127132
* @return true an error is contained
128133
* @return false an error is not contained
129134
*/
130-
constexpr bool has_error() {
135+
constexpr bool has_error() const noexcept {
131136
return error.has_value();
132137
}
133138

@@ -139,12 +144,12 @@ class Result {
139144
* @return true error is contained
140145
* @return false error is not contained
141146
*/
142-
constexpr bool contains(E error) {
147+
constexpr bool contains(E error) const noexcept(noexcept(std::declval<E>() == std::declval<E>())) {
143148
return this->error == error;
144149
}
145150

146151
// prevent ambiguous operator overload resolution
147152
bool operator==(const Result& other) = delete;
148153
};
149154

150-
} // namespace zest
155+
} // namespace zest

0 commit comments

Comments
 (0)