4 ms·
> Out-parameters are a terrible choice in C++ why? I personally love functions like `EResult function(const T1& inParam1, T2& outParam2)`. the function signatu
by leetcrew 8y ago
> Out-parameters are a terrible choice in C++
why? I personally love functions like `EResult function(const T1& inParam1, T2& outParam2)`. the function signature makes it obvious what the function does (or should do, at least) and it's easy to avoid accidental copying.
- ncmncm 8y agoCode like that is the bane of my existence. Give me `T2 function(T1)` every time. Besides being overwhelmingly clearer, I can use it in just one line. The other needs at least 3, more commonly 5, lines packed around it, so less than 20% of your code actually does anything useful toward the problem it is supposed to solve. Five times longer means you have five times the bugs. Besides being horrible style, operating on references cripples the optimizer. "Avoid accidental copying"? What good is that if it's slower than if you did the copy?
- signa11 8y agoserious question: would you throw an exception if T2 cannot be created ? or use a variant data-type ? or something else altogether?
- bigcheesegs 8y agoUse a variant type like llvm::Expected.
- signa11 8y agoyup, that's what i thought. i generally tend to use a construct simiar to 'error_or' though. that wrapped within a macro (^^) makes the whole invokation quite bearable
- ncmncm 8y agoGenerally, yes, throw. Sometimes an optional is better, such as when downstream already takes one. Occasionally, checking whether the operation (and a bunch of others) would succeed is best, particularly in high-rel/high-av systems. There, you are probably also logging, first, what you are about to attempt.