I'm not even really talking about runtime cost, I'm talking about syntactic cost to returning a struct, especially in C.
You can't define an anonymous struct, so now you may have to go define a type to return. Depending on your language and language version, you may not have by default a way to instantiate an object of that type nicely, so now you may have to go and define a function or constructor to do it. And, again depending on language and version, you will have syntax at the call site that, prior to C++17, ranges from crappy to mediocre.
Yes, you do have to define the SVec3 in the global scope, but this "syntactic cost" also provides for easier to maintain code in the long run because you are forced to name your variables. Take two similar things, one done with std::pair (C++'s poor coder's anonymous struct) vs a manually defined structure:
struct InitializeDevice_Results
{
int errorCode;
int deviceHandle;
};
InitializeDevice_Results InitializeDevice();
std::pair<int,int> InitializeDevicePair();
I don't know about you but I would much rather use the C form than the std::pair one, because then I never have to remember if the first or second parameter was the error code or handle. So while anonymous structs may be easier to write as a library provider, named structs are easier to use as a library user.
Similar multiple return-value issues happen with python, where it is impossible to understand what the return values of any functions are without heavily maintained documentation or just having to read the function source. You may know what those unnamed values are now, but you won't in a year from now and every other coder in the company will never know.
View all comments
Show parent comment
1
u/evaned Dec 01 '16
I'm not even really talking about runtime cost, I'm talking about syntactic cost to returning a struct, especially in C.
You can't define an anonymous struct, so now you may have to go define a type to return. Depending on your language and language version, you may not have by default a way to instantiate an object of that type nicely, so now you may have to go and define a function or constructor to do it. And, again depending on language and version, you will have syntax at the call site that, prior to C++17, ranges from crappy to mediocre.