Merge pull request #2405 from karzhenkov/fix-json_ref-move

Fix move constructor of json_ref
This commit is contained in:
Niels Lohmann 2020-12-11 13:36:33 +01:00 committed by GitHub
commit 3ad6992f50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 28 deletions

View File

@ -17,19 +17,14 @@ class json_ref
json_ref(value_type&& value)
: owned_value(std::move(value))
, value_ref(&owned_value)
, is_rvalue(true)
{}
json_ref(const value_type& value)
: value_ref(const_cast<value_type*>(&value))
, is_rvalue(false)
: value_ref(&value)
{}
json_ref(std::initializer_list<json_ref> init)
: owned_value(init)
, value_ref(&owned_value)
, is_rvalue(true)
{}
template <
@ -37,8 +32,6 @@ class json_ref
enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
json_ref(Args && ... args)
: owned_value(std::forward<Args>(args)...)
, value_ref(&owned_value)
, is_rvalue(true)
{}
// class should be movable only
@ -50,27 +43,26 @@ class json_ref
value_type moved_or_copied() const
{
if (is_rvalue)
if (value_ref == nullptr)
{
return std::move(*value_ref);
return std::move(owned_value);
}
return *value_ref;
}
value_type const& operator*() const
{
return *static_cast<value_type const*>(value_ref);
return value_ref ? *value_ref : owned_value;
}
value_type const* operator->() const
{
return static_cast<value_type const*>(value_ref);
return &**this;
}
private:
mutable value_type owned_value = nullptr;
value_type* value_ref = nullptr;
const bool is_rvalue = true;
value_type const* value_ref = nullptr;
};
} // namespace detail
} // namespace nlohmann

View File

@ -12605,19 +12605,14 @@ class json_ref
json_ref(value_type&& value)
: owned_value(std::move(value))
, value_ref(&owned_value)
, is_rvalue(true)
{}
json_ref(const value_type& value)
: value_ref(const_cast<value_type*>(&value))
, is_rvalue(false)
: value_ref(&value)
{}
json_ref(std::initializer_list<json_ref> init)
: owned_value(init)
, value_ref(&owned_value)
, is_rvalue(true)
{}
template <
@ -12625,8 +12620,6 @@ class json_ref
enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
json_ref(Args && ... args)
: owned_value(std::forward<Args>(args)...)
, value_ref(&owned_value)
, is_rvalue(true)
{}
// class should be movable only
@ -12638,27 +12631,26 @@ class json_ref
value_type moved_or_copied() const
{
if (is_rvalue)
if (value_ref == nullptr)
{
return std::move(*value_ref);
return std::move(owned_value);
}
return *value_ref;
}
value_type const& operator*() const
{
return *static_cast<value_type const*>(value_ref);
return value_ref ? *value_ref : owned_value;
}
value_type const* operator->() const
{
return static_cast<value_type const*>(value_ref);
return &**this;
}
private:
mutable value_type owned_value = nullptr;
value_type* value_ref = nullptr;
const bool is_rvalue = true;
value_type const* value_ref = nullptr;
};
} // namespace detail
} // namespace nlohmann