Skip to content

Commit

Permalink
Mark some folly::Try functions are noexcept
Browse files Browse the repository at this point in the history
Summary:
These functions are intended not to throw and they do not throw, so lets mark them as non-throwing.

`hasValue()` and `hasException()` are self-explanatory.

`tryGetExceptionObject()` returns `nullptr` on failure, which is similarly self-explanatory.

`makeTryWith(F&&)` is intended not to throw and never throws in practice. Caveat: this function never throws in practice as of C++17 and its syntactic copy-elision of returned temporaries, but may have thrown in practice before C++17 for `Try<T>` in cases where `T` is not noexcept-move-constructible.

Reviewed By: yfeldblum

Differential Revision: D62713625

fbshipit-source-id: 9e91fee5b56da5c366dc6120ed5648afbae6e02c
  • Loading branch information
praihan authored and facebook-github-bot committed Sep 16, 2024
1 parent b6e67d7 commit c6b96b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
8 changes: 4 additions & 4 deletions folly/Try-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ template <typename F>
typename std::enable_if<
!std::is_same<invoke_result_t<F>, void>::value,
Try<invoke_result_t<F>>>::type
makeTryWithNoUnwrap(F&& f) {
makeTryWithNoUnwrap(F&& f) noexcept {
using ResultType = invoke_result_t<F>;
try {
return Try<ResultType>(f());
Expand All @@ -260,7 +260,7 @@ makeTryWithNoUnwrap(F&& f) {
template <typename F>
typename std::
enable_if<std::is_same<invoke_result_t<F>, void>::value, Try<void>>::type
makeTryWithNoUnwrap(F&& f) {
makeTryWithNoUnwrap(F&& f) noexcept {
try {
f();
return Try<void>();
Expand All @@ -272,14 +272,14 @@ typename std::
template <typename F>
typename std::
enable_if<!isTry<invoke_result_t<F>>::value, Try<invoke_result_t<F>>>::type
makeTryWith(F&& f) {
makeTryWith(F&& f) noexcept {
return makeTryWithNoUnwrap(std::forward<F>(f));
}

template <typename F>
typename std::enable_if<isTry<invoke_result_t<F>>::value, invoke_result_t<F>>::
type
makeTryWith(F&& f) {
makeTryWith(F&& f) noexcept {
using ResultType = invoke_result_t<F>;
try {
return f();
Expand Down
38 changes: 20 additions & 18 deletions folly/Try.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,19 @@ class Try : detail::TryBase<T>,
/*
* @returns True if the Try contains a value, false otherwise
*/
bool hasValue() const { return this->contains_ == Contains::VALUE; }
bool hasValue() const noexcept { return this->contains_ == Contains::VALUE; }
/*
* @returns True if the Try contains an exception, false otherwise
*/
bool hasException() const { return this->contains_ == Contains::EXCEPTION; }
bool hasException() const noexcept {
return this->contains_ == Contains::EXCEPTION;
}

/*
* @returns True if the Try contains an exception of type Ex, false otherwise
*/
template <class Ex>
bool hasException() const {
bool hasException() const noexcept {
return hasException() && this->e_.template is_compatible_with<Ex>();
}

Expand Down Expand Up @@ -317,10 +319,10 @@ class Try : detail::TryBase<T>,
* @returns a pointer to the `std::exception` held by `*this`, if one is held;
* otherwise, returns `nullptr`.
*/
std::exception* tryGetExceptionObject() {
std::exception* tryGetExceptionObject() noexcept {
return hasException() ? this->e_.get_exception() : nullptr;
}
std::exception const* tryGetExceptionObject() const {
std::exception const* tryGetExceptionObject() const noexcept {
return hasException() ? this->e_.get_exception() : nullptr;
}

Expand All @@ -330,11 +332,11 @@ class Try : detail::TryBase<T>,
* returns `nullptr`.
*/
template <class Ex>
Ex* tryGetExceptionObject() {
Ex* tryGetExceptionObject() noexcept {
return hasException() ? this->e_.template get_exception<Ex>() : nullptr;
}
template <class Ex>
Ex const* tryGetExceptionObject() const {
Ex const* tryGetExceptionObject() const noexcept {
return hasException() ? this->e_.template get_exception<Ex>() : nullptr;
}

Expand Down Expand Up @@ -475,13 +477,13 @@ class Try<void> {
inline void throwUnlessValue() const;

// @returns False if the Try contains an exception, true otherwise
bool hasValue() const { return hasValue_; }
bool hasValue() const noexcept { return hasValue_; }
// @returns True if the Try contains an exception, false otherwise
bool hasException() const { return !hasValue_; }
bool hasException() const noexcept { return !hasValue_; }

// @returns True if the Try contains an exception of type Ex, false otherwise
template <class Ex>
bool hasException() const {
bool hasException() const noexcept {
return hasException() && e_.is_compatible_with<Ex>();
}

Expand Down Expand Up @@ -522,10 +524,10 @@ class Try<void> {
* @returns a pointer to the `std::exception` held by `*this`, if one is held;
* otherwise, returns `nullptr`.
*/
std::exception* tryGetExceptionObject() {
std::exception* tryGetExceptionObject() noexcept {
return hasException() ? e_.get_exception() : nullptr;
}
std::exception const* tryGetExceptionObject() const {
std::exception const* tryGetExceptionObject() const noexcept {
return hasException() ? e_.get_exception() : nullptr;
}

Expand All @@ -535,11 +537,11 @@ class Try<void> {
* returns `nullptr`.
*/
template <class E>
E* tryGetExceptionObject() {
E* tryGetExceptionObject() noexcept {
return hasException() ? e_.get_exception<E>() : nullptr;
}
template <class E>
E const* tryGetExceptionObject() const {
E const* tryGetExceptionObject() const noexcept {
return hasException() ? e_.get_exception<E>() : nullptr;
}

Expand Down Expand Up @@ -615,7 +617,7 @@ template <typename F>
typename std::enable_if<
!std::is_same<invoke_result_t<F>, void>::value,
Try<invoke_result_t<F>>>::type
makeTryWithNoUnwrap(F&& f);
makeTryWithNoUnwrap(F&& f) noexcept;

/*
* Specialization of makeTryWith for void return
Expand All @@ -627,7 +629,7 @@ makeTryWithNoUnwrap(F&& f);
template <typename F>
typename std::
enable_if<std::is_same<invoke_result_t<F>, void>::value, Try<void>>::type
makeTryWithNoUnwrap(F&& f);
makeTryWithNoUnwrap(F&& f) noexcept;

/*
* @param f a function to execute and capture the result of (value or exception)
Expand All @@ -637,7 +639,7 @@ typename std::
template <typename F>
typename std::
enable_if<!isTry<invoke_result_t<F>>::value, Try<invoke_result_t<F>>>::type
makeTryWith(F&& f);
makeTryWith(F&& f) noexcept;

/*
* Specialization of makeTryWith for functions that return Try<T>
Expand All @@ -651,7 +653,7 @@ typename std::
template <typename F>
typename std::enable_if<isTry<invoke_result_t<F>>::value, invoke_result_t<F>>::
type
makeTryWith(F&& f);
makeTryWith(F&& f) noexcept;

/*
* Try to in-place construct a new value from the specified arguments.
Expand Down

0 comments on commit c6b96b2

Please sign in to comment.