I would like to ask how to return an std::optional
in an efficient way and I would like to use std::make_optional()
.
For example lets have this code snippet:
std::optional<Path> CreateCanonicalPath(const std::string_view& path)
{
std::error_code errorCode;
const auto result = std::filesystem::weakly_canonical(std::filesystem::u8path(path), errorCode);
return !errorCode ? std::make_optional(result) : std::nullopt;
}
I am particularly interested whether there is any optimization in passing result
to std::make_optional
. Would it be better to use std::make_optional(std::move(result))
?
And does it prevent any RVO or NVRO?
The result
is a local variable but it is not exactly in a return statement so I assume the compiler can't use move by itself.