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; }

?

Follow

@hanken it does? I though it makes it cheaper... sans a rookie mistake on my part, see this thread
qoto.org/web/statuses/10683944

@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
qoto.org/web/statuses/10686274

@namark
Wow. This is good!
After looking at your answer I thought, sure, the return types might not be same. But I checked it and wow. add1 is not returning T! Only add2 does.

Thanks for sharing this.

@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.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.