我想知道这是否被认为是std :: optional的有效用法。我有一个返回process_id(std::uint32_t值)的std::uint32_t函数,如果没有找到目标进程ID,返回一个标准的“ ”函数会更有效吗?或者返回一个std :: optional更合适吗?
例:
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
}
Run Code Online (Sandbox Code Playgroud)
在返回unique_ptr以及返回nullptr的同时,我也在这样做,但是我不确定是否将其视为所述功能的“滥用”,并且最好返回0并检查该值是否更好。先感谢您。