0

Recently I posted this question : Ambigous operator overload on clang which got closed for being essentially the same as Is clang wrongfully reporting ambiguity when mixing member and non-member binary operators?

After reading answers to that other question, I think I am still confused about what more specialized means. So based on what I read there, the member operator from my code should resolve to this :

template <class U>
TVector3<T>& TVector3<float>::operator*=(const U ) { return *this; }

during name lookup and for the purpose of overload resolution I would get these two candidates:

template <class U>
TVector3<T>& operator*=(TVector3<float>&, const U ) { return *this; }

template <typename T>
TVector3<T>& operator*=(TVector3<T>& v, const TMatrix3<T>& ) { return v; }

Why are they both equally well specialized ?

Is it because in the first overload the first argument is more specialized, while in the second overload the second argument is more specialized ? So basically a tie between them ?

On the other hand, if I turned the member operator*= of the Vector class into a free function, the overload resolution set would be:

template <class T, class U>
TVector3<T>& operator*=(TVector3<T>& v, const U ) { return v; }

template <typename T>
TVector3<T>& operator*=(TVector3<T>& v, const TMatrix3<T>& ) { return v; }

in which case the first argument of both overloads is equally specialized, but the second argument of the second overload is more specialized and thus no ambiguity in this case.

Is my understanding correct ?

jcxz
  • 1,226
  • 2
  • 12
  • 25
  • There is no equally specialized; there neither is not more specialized. Insane, but... And non template methods of template classes are not template methods. A member of a template is just a member, not a template st overload resolution time. Ditto for free friend functions. This matters, because on ties, non templates win. – Yakk - Adam Nevraumont Oct 30 '21 at 04:10
  • @Yakk-AdamNevraumont Sorry, I am losing myself in your comment. In my code one operator is a **member template** and the other is a **free function template** – jcxz Oct 30 '21 at 08:58
  • `template TVector3& operator*=(TVector3& v, const TMatrix3& ) { return v; }` this, as a member of a template, isn't going to be a template. This, as a free function, could either be a template or not (if you use the inline friend trick, it won't be). A [mcve] might be of use? – Yakk - Adam Nevraumont Oct 30 '21 at 13:40
  • because even following your links (which should be supplementary, not core to understanding), I really don't know what code you are attempting to do overload resolution on in your first problem. When you don't understand what is going on, [mcve] are important, because if you knew what was relevant you'd probably also know your answer. – Yakk - Adam Nevraumont Oct 30 '21 at 13:46

0 Answers0