You can also wrap/group them in a dedicated struct/class and return that instead. That can typically produce code that’s cleaner and easier to read. Take a look at this for example would you prefer
bool DoSomething(int32& outResult) { ... }
or
TOptional<int32> DoSomething() { ... }
The TOptional
version states pretty clearly that it may return an int32 but its up to the caller to check and handle if it actually did. Internally TOptional
automatically tracks the bool whether it currently contains a value of the given type (int32 in this case). Whereas with the other version there is nothing that prevents you from using outResult
without first checking the return value itself.