KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
This is related to this answer provided by Matthieu M. on how to utilize move semantics with the + operator overloading (in general, operators which don't re-assign directly back to the left param). He suggested implementing three distinct overloads: inline T operator+(T left, T const& right) { left += right; return left; } inline T operator+(T const& left, T right) { right += left; return right; } // commutative inline T operator+(T left, T&& right) { left += right; return left; } // disambiguation Number 1 and 3 make sense, but I don't understand what purpose 2 does. The comment suggests commutative handling, but it seems that 1 and 2 would be mutually exclusive (i.e. implementing both results in ambiguities) For example, with all 3 implemented: T a, b, c; c = a + b; Compiler output: 1> error C2593: 'operator +' is ambiguous 1> could be 'T operator +(const T &,T)' 1> or 'T operator +(T,const T &)' 1> while trying to match the argument list '(T, T)' removing either 1 or 2 and the program works as expected. Since 1 is the general case and 2 only works correctly with a commutative operator, I don't see why 2 would ever be used. Is there something I'm missing?
Tags (comma-separated)
Save Edits
Cancel