1

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 :

  1. should my overloaded operator have explicit specialization?

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

GGinside
  • 63
  • 6
  • I didn't read the other post, but `A& operator=(const A& x)` looks like regular copy assignment. Nothing special needed - but the conversion assignment in `operator=(T& x)` could possibly be removed to leave the job to the converting constructor. How come you don't set `m_others = 1` in the conversion assignment operator? It'll be left with the old value. – Ted Lyngmo Jun 25 '20 at 19:28
  • `T x=0` as default seems unnecessary. `T x = T{}` would probably do, but it may be better to separate the default constructor from the conversion constructor. – Ted Lyngmo Jun 25 '20 at 19:33
  • @TedLyngmo That is the way I want it. I just want to update m_object with new values. – GGinside Jun 25 '20 at 20:02
  • Ok, that's utterly confusing for a user. Copy constructing and copy assignment does not work "in tandem" so to speak. `A a1{1,2};` `A a2{1};` `a1 = 1;` now `(a1 != a2) == true`. – Ted Lyngmo Jun 25 '20 at 20:18
  • I would overload everything, every operator, that is the intention of this class originally. I want add/substr/multiply/assign/whatever operation in my defined way. So if there is a problem with !=, it would be overloaded and solved. – GGinside Jun 25 '20 at 21:05
  • @TedLyngmo since you are asking, I would be very interested to learn if the way i am doing things is inappropriate? Seems C++ is very complex, and I am just using my imagination in many places.. – GGinside Jun 25 '20 at 21:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216684/discussion-between-gginside-and-ted-lyngmo). – GGinside Jun 25 '20 at 23:44
  • There aren’t even any (member) function templates here to specialize. You probably need to edit your question to simply ask “What is my code doing here?”. – Davis Herring Jun 26 '20 at 02:18
  • @DavisHerring i overloaded my operator with a specialization here? Isn't it specialization? So how does compiler know to use A& operator=(const T& x) or A& operator=(const A& x)? Since the template type could be A or it could be something else? – GGinside Jun 26 '20 at 04:31
  • @GGinside: Sorry, but the answers to those questions are “no, no, overload resolution, and a possible error”; that’s why I said the question needed editing. – Davis Herring Jun 26 '20 at 05:10
  • @DavisHerring Could you kindly let me know what I am doing? That is what I am trying to figure out. So all of you are telling me this is not overloading,but i am overloading the assignment operator? If not, what am i doing? – GGinside Jun 26 '20 at 05:22
  • @DavisHerring I agree that there is no specialization because I am asking should I add it? And an error refers to? Could you kindly elaborate? Since I am getting really confused. – GGinside Jun 26 '20 at 05:24

1 Answers1

0

A<T>::operator=(const T&) & is templated (because it’s part of a class template), but it is not a template. As such, it’s impossible to specialize it (except “as a member of A”, which is a rare and irrelevant case). Each specialization of A has two assignment operators; note that it’s impossible for T to be A (and cause a conflict between the two operators) because A is a template, not a type. (Nor can you construct the self-referential type A<A<A<…>>>.) Normal overload resolution takes place for each assignment to decide which one to call.

Also note that your A<T>::operator=(const A&) is all but identical to the one that would be implicitly declared if you but omitted it. (Declaring the other doesn’t prevent that: all classes have a copy assignment operator.)

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • thanks a lot for answering! Template programming is giving me a headache... I mean I did declare A::operator=(const A&) which seems kindly irrelevant, but it is just related to my question that i was confused about, so it's here just to illustrate my question...or you would be even more confused about what i am asking for.. but you did solve my concerns! Thanks again! – GGinside Jun 26 '20 at 07:20
  • should i start another question for whhat would be the use case of explicit specialization? or you could kindly add that in your answer as well? – GGinside Jun 26 '20 at 07:22
  • @GGinside: That should definitely be its own question, but surely it’s been asked several times already. – Davis Herring Jun 26 '20 at 12:35
  • cool. Thanks. I searched, and saw those answers, and none of them is clear enough, such as the one i give as example, does not say much. Also, the dedicated copy construct is explicit for another reason is my real class is not simply 2 int member vars, it has some other objects in it that need to be copied. So it is there just for this question.. – GGinside Jun 26 '20 at 21:41