From 31876b4efd5efcd8517d57fb998aed915bfdd6d4 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Tue, 5 Feb 2013 08:22:27 +0000 Subject: [PATCH] [Support][ErrorOr] Add support for convertable types. llvm-svn: 174357 --- llvm/include/llvm/Support/ErrorOr.h | 8 +++++--- llvm/unittests/Support/ErrorOrTest.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/Support/ErrorOr.h b/llvm/include/llvm/Support/ErrorOr.h index 452d85ceecfb..653b9e5c88f2 100644 --- a/llvm/include/llvm/Support/ErrorOr.h +++ b/llvm/include/llvm/Support/ErrorOr.h @@ -162,6 +162,7 @@ public: /// T cannot be a rvalue reference. template class ErrorOr { + template friend class ErrorOr; static const bool isRef = is_reference::value; typedef ReferenceStorage::type> wrap; @@ -198,7 +199,8 @@ public: new (get()) storage_type(moveIfMoveConstructible(Val)); } - ErrorOr(const ErrorOr &Other) : IsValid(false) { + template + ErrorOr(ErrorOr &Other) : IsValid(false) { // Construct an invalid ErrorOr if other is invalid. if (!Other.IsValid) return; @@ -227,7 +229,8 @@ public: } #if LLVM_HAS_RVALUE_REFERENCES - ErrorOr(ErrorOr &&Other) : IsValid(false) { + template + ErrorOr(ErrorOr &&Other) : IsValid(false) { // Construct an invalid ErrorOr if other is invalid. if (!Other.IsValid) return; @@ -311,7 +314,6 @@ private: return &Val->get(); } -protected: storage_type *get() { assert(IsValid && "Can't do anything on a default constructed ErrorOr!"); assert(!HasError && "Cannot get value when an error exists!"); diff --git a/llvm/unittests/Support/ErrorOrTest.cpp b/llvm/unittests/Support/ErrorOrTest.cpp index a8608860b84b..6cef4fc212f3 100644 --- a/llvm/unittests/Support/ErrorOrTest.cpp +++ b/llvm/unittests/Support/ErrorOrTest.cpp @@ -53,6 +53,17 @@ TEST(ErrorOr, Types) { EXPECT_EQ(3, **t3()); #endif } + +struct B {}; +struct D : B {}; + +TEST(ErrorOr, Covariant) { + ErrorOr b(ErrorOr(0)); + +#if LLVM_HAS_CXX11_STDLIB + ErrorOr > b1(ErrorOr >(0)); +#endif +} } // end anon namespace struct InvalidArgError {