

If you never use them again, this is frustrating. This isn't awful, but it could result in your code being littered with oddly named little structs like twoNumbers and threeStrings and twoNumbersandaString and the like. Listing 1: One Struct to Hold Multiple Values It's not hard, as you can tell by the code in Listing 1. If you're one of the many developers who doesn't like or use the STL, then your go-to alternative is to write a small class or struct to hold the multiple values you'd like to return. We could argue all day about the best way to use out parameters, but I'm much happier telling you not to use out parameters at all. Still, others decide not to even have a return value and only use out parameters. Others use the return for the most important result, and out parameters for messages, success or failure flags, and secondary results. Some developers use the return for a Boolean indicating success or failure, and out parameters for all the calculation results. It's not clear here (or even in slightly less artificial functions) why one result of the function gets to be the return value and others have to be out parameters. It's relatively ugly and irritating, and relies on comments or API documentation so that the consumer of the function can use it properly. This means the calling code needs to create a variable first, and then pass it in by reference. What are you being asked to prefer returning: a tuple or a struct over? What other option would you have? Typically, the other option is some sort of out parameter - a parameter you take by non-const reference and change within the function. They're all good, but I want to show you F.21: To return multiple "out" values, prefer returning a tuple or struct.īefore I explain what a tuple is, I want to address the implied assumption in that guideline. There are more than 50 guidelines relating to functions (and that's not counting sections on special functions like constructors and destructors). I'd like to show you some pretty useful templates from the Standard Template Library (STL) that enable you to get around the restriction that you can only return one value from a function.
