ARTICLE AD BOX
I have a function like that which produces -Wreduntant-move:
std::optional<std::string> func(const bool f) { std::string res; if (f) return {}; else res += "asd"; return std::move(res); }std::optional has a constructor, which without std::move should lead to copy of res
template< class U = std::remove_cv_t<T> > constexpr optional( U&& value );Also i want to mention, that
MSVC/Clang does not produce any warning here Adding {}(return { std::move(res) };) fixes the warning Returnnig any of res, std::move(res), { std::move(res) } produces the same assembly with any O*Help me understand, please
Why the warning is produced Why there is no copy in ctor with res Does the RVO implies here(should not be, because the decltype(res) differes for function return type, but according to the assembly - yes)? If not, how can be this code be rewritten to enable RVO?