Is there an effective difference between std::move(*optional) and *std::move(optional)? Which one is preferable?
Full example:
#include <optional>
#include <vector>
void foo()
{
std::optional<std::vector<int>> ov = std::vector<int>{};
std::vector<int> v;
v = std::move(*ov);
}
void bar()
{
std::optional<std::vector<int>> ov = std::vector<int>{};
std::vector<int> v;
v = *std::move(ov);
}
They do the same thing.
In
v = std::move(*ov);,*ovis astd::vector<int>&sostd::move(*ov)gives you astd::vector<int>&&that you are trying to assign tov.In
v = *std::move(ov);ovis astd::optional<std::vector<int>>sostd::move(ov)gives you astd::optional<std::vector<int>>&&and calling*on that callsconstexpr T&& operator*() && noexcept;so you again have astd::vector<int>&&that you are trying to assign tovPersonally I prefer
v = std::move(*ov);asv = *std::move(ov);makes me go: "Hmm, I am dereferencing, do I have an lvalue like most dereferences give or do I really have an rvalue?" and I would rather not have to ask myself that.