template <class T>
Try<T>::Try(Try<T>&& t) : contains_(t.contains_) {
- if (contains_ == VALUE) {
+ if (contains_ == Contains::VALUE) {
new (&value_)T(std::move(t.value_));
- } else if (contains_ == EXCEPTION) {
+ } else if (contains_ == Contains::EXCEPTION) {
new (&e_)std::exception_ptr(t.e_);
}
}
Try<T>& Try<T>::operator=(Try<T>&& t) {
this->~Try();
contains_ = t.contains_;
- if (contains_ == VALUE) {
+ if (contains_ == Contains::VALUE) {
new (&value_)T(std::move(t.value_));
- } else if (contains_ == EXCEPTION) {
+ } else if (contains_ == Contains::EXCEPTION) {
new (&e_)std::exception_ptr(t.e_);
}
return *this;
template <class T>
Try<T>::~Try() {
- if (contains_ == VALUE) {
+ if (contains_ == Contains::VALUE) {
value_.~T();
- } else if (contains_ == EXCEPTION) {
+ } else if (contains_ == Contains::EXCEPTION) {
e_.~exception_ptr();
}
}
template <class T>
void Try<T>::throwIfFailed() const {
- if (contains_ != VALUE) {
- if (contains_ == EXCEPTION) {
+ if (contains_ != Contains::VALUE) {
+ if (contains_ == Contains::EXCEPTION) {
std::rethrow_exception(e_);
} else {
throw UsingUninitializedTry();
static_assert(!std::is_reference<T>::value,
"Try may not be used with reference types");
- enum Contains {
+ enum class Contains {
VALUE,
EXCEPTION,
NOTHING,
public:
typedef T element_type;
- Try() : contains_(NOTHING) {}
- explicit Try(const T& v) : contains_(VALUE), value_(v) {}
- explicit Try(T&& v) : contains_(VALUE), value_(std::move(v)) {}
- explicit Try(std::exception_ptr e) : contains_(EXCEPTION), e_(e) {}
+ Try() : contains_(Contains::NOTHING) {}
+ explicit Try(const T& v) : contains_(Contains::VALUE), value_(v) {}
+ explicit Try(T&& v) : contains_(Contains::VALUE), value_(std::move(v)) {}
+ explicit Try(std::exception_ptr e) : contains_(Contains::EXCEPTION), e_(e) {}
// move
Try(Try<T>&& t);
const T* operator->() const { return &value(); }
T* operator->() { return &value(); }
- bool hasValue() const { return contains_ == VALUE; }
- bool hasException() const { return contains_ == EXCEPTION; }
+ bool hasValue() const { return contains_ == Contains::VALUE; }
+ bool hasException() const { return contains_ == Contains::EXCEPTION; }
private:
Contains contains_;