Confusion on template copy assignment function

49 Views Asked by At

[First of First: Vs2019 on Windows10, only C++11 supported]

I've got confused on template copy assignment function like: enter image description here

I found the specilization version is not working, why it is not equal to copy assignment function? isn't that template function will be "extended" like macro?

Let's add a move constructor like this: enter image description here

I know that a userd-declared move constructor will set the implicit copy assignment function to delete. if the template copy assignment function is not a "REAL" copy assignment function, then why the compiler says:

error C2280: 'TestClass &TestClass::operator =(const TestClass &)': attempting to reference a deleted function
message : compiler has generated 'TestClass::operator =' here
message : 'TestClass &TestClass::operator =(const TestClass &)': function was implicitly deleted because 'TestClass' has a user-defined move constructor

So, How does compiler see on the template copy assignment function? To be clear: If template-assign-operator function is not identical to copy-assign operator, why the compiler reminds me 'TestClass &TestClass::operator =(const TestClass &)': function was implicitly deleted

Thanks in advance!

I expect the specilization version of template copy assignment function to be identical to copy assignment function.

Full test code here:

class TestClass
{
    public:
    TestClass() {};
    ~TestClass() {};
    // If the template function is not identical to copy assignment function
    // why the compiler reports error:"function was implicitly deleted because 'TestClass' has a user-defined move constructor"
    TestClass(TestClass&& rhs)
    {

    }

    template <typename T>
    TestClass& operator = (const T& rhs)
    {
        return *this;
    }

    // Why this is not identical to copy assignment function?
    template<>
    TestClass& operator = (const TestClass& rhs)
    {
        return *this;
    }
};

int main()
{
    TestClass A;
    TestClass B;
    B = A;
}
0

There are 0 best solutions below