I was wondering if this would be considered a valid usage of std::optional. I have a function that returns a process_id
(std::uint32_t
value), would it be more efficient to have a standard "std::uint32_t
" function that returns 0 if we fail to find the target processes ID or would returning a std::optional be more appropriate?
example:
std::optional<std::uint32_t> FindProcessID(std::string_view process)
{
bool find = false;
if (!find)
// we fail to find the process_id and return nothing.
return std::nullopt;
else if (find)
return 100; // return the id
}
I am also doing this when returning a unique_ptr as-well opposed to just returning a nullptr, but I am unsure if this would be considered "abuse" of said feature, and if it would be better just to return 0 and check for that value. Thank you in advance.
unique_ptr
forces dynamic allocation when there's no good reason to do so for this use case. – Tavern