Bug 1625871 - InitializedOnce<const T> should be a literal type for literal types T. r=froydnj

Depends on D68758

Differential Revision: https://phabricator.services.mozilla.com/D68759

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Simon Giesecke 2020-03-30 12:27:10 +00:00
Родитель b30450b50d
Коммит df70dae59a
2 изменённых файлов: 22 добавлений и 14 удалений

Просмотреть файл

@ -50,7 +50,7 @@ class InitializedOnce final {
public:
template <typename Dummy = void>
explicit InitializedOnce(
explicit constexpr InitializedOnce(
std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy>* =
nullptr) {}
@ -58,7 +58,7 @@ class InitializedOnce final {
// arguments. The default constructor should only be available conditionally
// and is declared above.
template <typename Arg0, typename... Args>
explicit InitializedOnce(Arg0&& aArg0, Args&&... aArgs)
explicit constexpr InitializedOnce(Arg0&& aArg0, Args&&... aArgs)
: mMaybe{Some(std::remove_const_t<T>{std::forward<Arg0>(aArg0),
std::forward<Args>(aArgs)...})} {
MOZ_ASSERT(ValueCheckPolicy<T>::Check(*mMaybe));
@ -86,7 +86,7 @@ class InitializedOnce final {
}
template <typename... Args, typename Dummy = void>
std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy> init(
constexpr std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy> init(
Args&&... aArgs) {
MOZ_ASSERT(mMaybe.isNothing());
MOZ_ASSERT(!mWasReset);
@ -94,12 +94,12 @@ class InitializedOnce final {
MOZ_ASSERT(ValueCheckPolicy<T>::Check(*mMaybe));
}
explicit operator bool() const { return isSome(); }
bool isSome() const { return mMaybe.isSome(); }
bool isNothing() const { return mMaybe.isNothing(); }
constexpr explicit operator bool() const { return isSome(); }
constexpr bool isSome() const { return mMaybe.isSome(); }
constexpr bool isNothing() const { return mMaybe.isNothing(); }
T& operator*() const { return *mMaybe; }
T* operator->() const { return mMaybe.operator->(); }
constexpr T& operator*() const { return *mMaybe; }
constexpr T* operator->() const { return mMaybe.operator->(); }
template <typename Dummy = void>
std::enable_if_t<DestroyWhenVal == DestroyWhen::EarlyAllowed, Dummy>

Просмотреть файл

@ -27,10 +27,10 @@ void AssertIsNothing(const T& aVal) {
ASSERT_TRUE(aVal.isNothing());
}
// XXX TODO Should be true once Bug ... ensures this for Maybe.
// static_assert(std::is_trivially_destructible_v<InitializedOnce<const int>>);
// static_assert(std::is_trivially_default_constructible_v<InitializedOnce<const
// int, LazyInit::Allow>>);
static_assert(std::is_literal_type_v<InitializedOnce<const int>>);
static_assert(std::is_literal_type_v<LazyInitializedOnce<const int>>);
static_assert(std::is_trivially_destructible_v<InitializedOnce<const int>>);
static_assert(std::is_trivially_destructible_v<LazyInitializedOnce<const int>>);
static_assert(!std::is_copy_constructible_v<InitializedOnce<const int>>);
static_assert(!std::is_copy_assignable_v<InitializedOnce<const int>>);
@ -75,7 +75,7 @@ static_assert(
test_has_init_method(kPtrInitializedOnceIntLazyInitAllowResettable));
struct MoveOnly {
explicit MoveOnly(int aValue) : mValue{aValue} {}
explicit constexpr MoveOnly(int aValue) : mValue{aValue} {}
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator=(MoveOnly&&) = default;
@ -89,8 +89,16 @@ constexpr int testValue = 32;
TEST(InitializedOnce, ImmediateInit)
{
const InitializedOnce<const MoveOnly> val{testValue};
constexpr InitializedOnce<const MoveOnly> val{testValue};
// compile-time assertions
static_assert(val);
static_assert(val.isSome());
static_assert(!val.isNothing());
static_assert(testValue == (*val).mValue);
static_assert(testValue == val->mValue);
// run-time assertions
AssertIsSome(val);
ASSERT_EQ(testValue, (*val).mValue);
ASSERT_EQ(testValue, val->mValue);