[Support][ErrorOr] Add support for convertable types.

llvm-svn: 174357
This commit is contained in:
Michael J. Spencer 2013-02-05 08:22:27 +00:00
parent b9c5b1a5c4
commit 31876b4efd
2 changed files with 16 additions and 3 deletions

View File

@ -162,6 +162,7 @@ public:
/// T cannot be a rvalue reference. /// T cannot be a rvalue reference.
template<class T> template<class T>
class ErrorOr { class ErrorOr {
template <class OtherT> friend class ErrorOr;
static const bool isRef = is_reference<T>::value; static const bool isRef = is_reference<T>::value;
typedef ReferenceStorage<typename remove_reference<T>::type> wrap; typedef ReferenceStorage<typename remove_reference<T>::type> wrap;
@ -198,7 +199,8 @@ public:
new (get()) storage_type(moveIfMoveConstructible<storage_type>(Val)); new (get()) storage_type(moveIfMoveConstructible<storage_type>(Val));
} }
ErrorOr(const ErrorOr &Other) : IsValid(false) { template <class OtherT>
ErrorOr(ErrorOr<OtherT> &Other) : IsValid(false) {
// Construct an invalid ErrorOr if other is invalid. // Construct an invalid ErrorOr if other is invalid.
if (!Other.IsValid) if (!Other.IsValid)
return; return;
@ -227,7 +229,8 @@ public:
} }
#if LLVM_HAS_RVALUE_REFERENCES #if LLVM_HAS_RVALUE_REFERENCES
ErrorOr(ErrorOr &&Other) : IsValid(false) { template <class OtherT>
ErrorOr(ErrorOr<OtherT> &&Other) : IsValid(false) {
// Construct an invalid ErrorOr if other is invalid. // Construct an invalid ErrorOr if other is invalid.
if (!Other.IsValid) if (!Other.IsValid)
return; return;
@ -311,7 +314,6 @@ private:
return &Val->get(); return &Val->get();
} }
protected:
storage_type *get() { storage_type *get() {
assert(IsValid && "Can't do anything on a default constructed ErrorOr!"); assert(IsValid && "Can't do anything on a default constructed ErrorOr!");
assert(!HasError && "Cannot get value when an error exists!"); assert(!HasError && "Cannot get value when an error exists!");

View File

@ -53,6 +53,17 @@ TEST(ErrorOr, Types) {
EXPECT_EQ(3, **t3()); EXPECT_EQ(3, **t3());
#endif #endif
} }
struct B {};
struct D : B {};
TEST(ErrorOr, Covariant) {
ErrorOr<B*> b(ErrorOr<D*>(0));
#if LLVM_HAS_CXX11_STDLIB
ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(0));
#endif
}
} // end anon namespace } // end anon namespace
struct InvalidArgError { struct InvalidArgError {