mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-12-04 01:11:44 +00:00
Use std::forward to make ErrorOr<T> constructible from a value that has a user-defined conversion to T. No functionality change intended.
llvm-svn: 260196
This commit is contained in:
parent
506f4f8e68
commit
6c9fb2428f
@ -23,20 +23,6 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
template<class T, class V>
|
|
||||||
typename std::enable_if< std::is_constructible<T, V>::value
|
|
||||||
, typename std::remove_reference<V>::type>::type &&
|
|
||||||
moveIfMoveConstructible(V &Val) {
|
|
||||||
return std::move(Val);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T, class V>
|
|
||||||
typename std::enable_if< !std::is_constructible<T, V>::value
|
|
||||||
, typename std::remove_reference<V>::type>::type &
|
|
||||||
moveIfMoveConstructible(V &Val) {
|
|
||||||
return Val;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Stores a reference that can be changed.
|
/// \brief Stores a reference that can be changed.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class ReferenceStorage {
|
class ReferenceStorage {
|
||||||
@ -107,8 +93,12 @@ public:
|
|||||||
new (getErrorStorage()) std::error_code(EC);
|
new (getErrorStorage()) std::error_code(EC);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr(T Val) : HasError(false) {
|
template <class OtherT>
|
||||||
new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
|
ErrorOr(OtherT &&Val,
|
||||||
|
typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
|
||||||
|
* = nullptr)
|
||||||
|
: HasError(false) {
|
||||||
|
new (getStorage()) storage_type(std::forward<OtherT>(Val));
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr(const ErrorOr &Other) {
|
ErrorOr(const ErrorOr &Other) {
|
||||||
|
@ -16,7 +16,7 @@ using namespace llvm;
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
ErrorOr<int> t1() {return 1;}
|
ErrorOr<int> t1() { return 1; }
|
||||||
ErrorOr<int> t2() { return errc::invalid_argument; }
|
ErrorOr<int> t2() { return errc::invalid_argument; }
|
||||||
|
|
||||||
TEST(ErrorOr, SimpleValue) {
|
TEST(ErrorOr, SimpleValue) {
|
||||||
@ -71,6 +71,46 @@ TEST(ErrorOr, Comparison) {
|
|||||||
EXPECT_EQ(x, errc::no_such_file_or_directory);
|
EXPECT_EQ(x, errc::no_such_file_or_directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(ErrorOr, ImplicitConversion) {
|
||||||
|
ErrorOr<std::string> x("string literal");
|
||||||
|
EXPECT_TRUE(!!x);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ErrorOr, ImplicitConversionCausesMove) {
|
||||||
|
struct Source {};
|
||||||
|
struct Destination {
|
||||||
|
Destination(const Source&) {}
|
||||||
|
Destination(Source&&) = delete;
|
||||||
|
};
|
||||||
|
Source s;
|
||||||
|
ErrorOr<Destination> x = s;
|
||||||
|
EXPECT_TRUE(!!x);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ErrorOr, ImplicitConversionNoAmbiguity) {
|
||||||
|
struct CastsToErrorCode {
|
||||||
|
CastsToErrorCode() = default;
|
||||||
|
CastsToErrorCode(std::error_code) {}
|
||||||
|
operator std::error_code() { return errc::invalid_argument; }
|
||||||
|
} casts_to_error_code;
|
||||||
|
ErrorOr<CastsToErrorCode> x1(casts_to_error_code);
|
||||||
|
ErrorOr<CastsToErrorCode> x2 = casts_to_error_code;
|
||||||
|
ErrorOr<CastsToErrorCode> x3 = {casts_to_error_code};
|
||||||
|
ErrorOr<CastsToErrorCode> x4{casts_to_error_code};
|
||||||
|
ErrorOr<CastsToErrorCode> x5(errc::no_such_file_or_directory);
|
||||||
|
ErrorOr<CastsToErrorCode> x6 = errc::no_such_file_or_directory;
|
||||||
|
ErrorOr<CastsToErrorCode> x7 = {errc::no_such_file_or_directory};
|
||||||
|
ErrorOr<CastsToErrorCode> x8{errc::no_such_file_or_directory};
|
||||||
|
EXPECT_TRUE(!!x1);
|
||||||
|
EXPECT_TRUE(!!x2);
|
||||||
|
EXPECT_TRUE(!!x3);
|
||||||
|
EXPECT_TRUE(!!x4);
|
||||||
|
EXPECT_FALSE(x5);
|
||||||
|
EXPECT_FALSE(x6);
|
||||||
|
EXPECT_FALSE(x7);
|
||||||
|
EXPECT_FALSE(x8);
|
||||||
|
}
|
||||||
|
|
||||||
// ErrorOr<int*> x(nullptr);
|
// ErrorOr<int*> x(nullptr);
|
||||||
// ErrorOr<std::unique_ptr<int>> y = x; // invalid conversion
|
// ErrorOr<std::unique_ptr<int>> y = x; // invalid conversion
|
||||||
static_assert(
|
static_assert(
|
||||||
|
Loading…
Reference in New Issue
Block a user