what's the difference between
template<typename T>
auto add1(const T& a, const T& b)
{ return a + b; }
and
template<typename T>
auto add2(T a, const T& b)
{ return a += b; }
?
@namark
add2 makes chaining expensive?
@hanken it does? I though it makes it cheaper... sans a rookie mistake on my part, see this thread
https://qoto.org/web/statuses/106839448913933165
@namark
Well, I am no expert myself but add2 is a template function and a move constructor isn't guaranteed. So while RVO will take care of return values, in case of chained/nested function calls, e.g. add2(add2(a,b), b) temporaries will be created.
But this is moot, since you mention that this isn't the difference you are talking about.
I am eager to know what you have in mind.🙂
@hanken move not guaranteed as in T might not have a move constructor? Preposterous! I'm not changing my functions you fix your types :<
Anyhow, I posted my answer
https://qoto.org/web/statuses/106862742673984441
@hanken no one ever expects arithmetic type promotion! This is why you can't in general implement/de-duplicate/replace operator+ with operator+=, and it's even bigger deal when you take into account types that use expression templates, or have bounds or measurement units associated with them at compile time.