This is one answer i see and got confused
Based on the answer of this question, explicit specialization is needed because if a template function is changed, while the overloaded function did not change, then the overloaded function would get called silently. It is confusing, since I am wondering is that the only use case?
So in my case, do I need to have explicit specialization?
What is compiler's logic in determining which function to call? Is it by looking at both, and if a call just match the template A& operator=(const T& x) it uses it; otherwise, it found the input is operator=(const A& x), it uses this one over the one with template? Since both function also has the same signature except for the template type. So if the template is deduced at compile time, then I would have two functions with the same signature. This is why I am confusing. Is there a vtable for overloaded function/operator? What it uses to determine op2 over op1 when I call A a; A b; a = b?
template<typename T>
class A{
public:
explicit A(T x=0, uint32_t others=1) :
m_obj((int64_t)x), m_others(others) {}
// op1
A(const A& x) :
m_obj(x.m_obj),
m_bitWidth(x.others) {
}
//op 2
A& operator=(const T& x) & //for lvalue assignment
{
m_obj = x;
return *this;
}
A& operator=(const A& x) { //specialized? overload? for A type objects
if(x != this) {
m_obj = x.m_obj;
m_others = x.m_others;
}
return *this;
}
double m_obj;
double m_others;
};
The reason I have operator=(T& x) and operator=(const A& x) is because I want to be able to do the following:
A<int> a;
A<int> b(10,20);
int c = 10;
a = b;
a = 10;
So my question would be :
should my overloaded operator have explicit specialization?
if explicit specialization is not needed, what exactly is explicit specialization? What are some use cases?
template<>
A& operator=(const A& x) { //specialized? overload? for A type objects
if(x != this) {
m_obj = x.m_obj;
m_others = x.m_others;
}
return *this;
}
Edit