Error C2280 occurs in class template function in Visual Studio 2022 The compiler reports that the function is deleted because a data member of type Reference is used
Classes containing reference type data members can be compiled and executed normally
class CReference {
public:
double& m_Reference;
CReference(double& Data) :
m_Reference(Data)
{
}
virtual ~CReference() {};
CReference(const CReference& Obj) :
m_Reference(Obj.m_Reference) {
}
CReference& operator = (const CReference& Obj) {
m_Reference = Obj.m_Reference;
return *this;
}
};
int main() {
double e0 = 1.0, e1 = 2.0;
CReference R0(e0);
CReference R1(e1);
CReference R2(R0);
R2 = R1;
}
Template classes and template functions can also be compiled and executed normally
template <typename Type>
class CVectorReference2 {
public:
Type& m_x, & m_y;
Type& m_s, & m_t;
CVectorReference2(Type& Value0, Type& Value1) :
m_x(Value0), m_y(Value1),
m_s(Value0), m_t(Value1) { }
virtual ~CVectorReference2() {}
CVectorReference2(const CVectorReference2<Type>& Vector) :
m_x(Vector.m_x), m_y(Vector.m_y),
m_s(Vector.m_s), m_t(Vector.m_t) { }
CVectorReference2<Type>& operator=(const CVectorReference2<Type>& Vector) {
Assign(Vector);
return *this;
}
template<typename InputType>
inline void Assign(const CVectorReference2<InputType>& Vector) {
m_x = (Type)Vector.m_x;
m_y = (Type)Vector.m_y;
}
};
int main() {
double e0, e1, e2 = 1.0, e3 = 2.0;
CVectorReference2<double> P0(e0, e1);
CVectorReference2<double> P1(e2, e3);
CVectorReference2<double> P2(P0);
P2 = P1;
}
But when operator= is replaced with template function, Error C2280 is reported saying that reference type data members are used? why?
This error message is really strange, how can I troubleshoot it?
template <typename Type>
class CVectorReference2 {
public:
Type& m_x, & m_y;
Type& m_s, & m_t;
CVectorReference2(Type& Value0, Type& Value1) :
m_x(Value0), m_y(Value1),
m_s(Value0), m_t(Value1) { }
virtual ~CVectorReference2() {}
CVectorReference2(const CVectorReference2<Type>& Vector) :
m_x(Vector.m_x), m_y(Vector.m_y),
m_s(Vector.m_s), m_t(Vector.m_t) { }
template<typename InputType> //<-------
CVectorReference2<Type>& operator=(const CVectorReference2<InputType>& Vector) {
Assign(Vector);
return *this;
}
template<typename InputType>
inline void Assign(const CVectorReference2<InputType>& Vector) {
m_x = (Type)Vector.m_x;
m_y = (Type)Vector.m_y;
}
};
int main() {
double e0, e1, e2 = 1.0, e3 = 2.0;
CVectorReference2<double> P0(e0, e1);
CVectorReference2<double> P1(e2, e3);
CVectorReference2<double> P2(P0);
P2 = P1;
}
Error C2280 occurs in class template function in Visual Studio 2022 The compiler reports that the function is deleted because a data member of type Reference is used
Classes containing reference type data members can be compiled and executed normally
class CReference {
public:
double& m_Reference;
CReference(double& Data) :
m_Reference(Data)
{
}
virtual ~CReference() {};
CReference(const CReference& Obj) :
m_Reference(Obj.m_Reference) {
}
CReference& operator = (const CReference& Obj) {
m_Reference = Obj.m_Reference;
return *this;
}
};
int main() {
double e0 = 1.0, e1 = 2.0;
CReference R0(e0);
CReference R1(e1);
CReference R2(R0);
R2 = R1;
}
Template classes and template functions can also be compiled and executed normally
template <typename Type>
class CVectorReference2 {
public:
Type& m_x, & m_y;
Type& m_s, & m_t;
CVectorReference2(Type& Value0, Type& Value1) :
m_x(Value0), m_y(Value1),
m_s(Value0), m_t(Value1) { }
virtual ~CVectorReference2() {}
CVectorReference2(const CVectorReference2<Type>& Vector) :
m_x(Vector.m_x), m_y(Vector.m_y),
m_s(Vector.m_s), m_t(Vector.m_t) { }
CVectorReference2<Type>& operator=(const CVectorReference2<Type>& Vector) {
Assign(Vector);
return *this;
}
template<typename InputType>
inline void Assign(const CVectorReference2<InputType>& Vector) {
m_x = (Type)Vector.m_x;
m_y = (Type)Vector.m_y;
}
};
int main() {
double e0, e1, e2 = 1.0, e3 = 2.0;
CVectorReference2<double> P0(e0, e1);
CVectorReference2<double> P1(e2, e3);
CVectorReference2<double> P2(P0);
P2 = P1;
}
But when operator= is replaced with template function, Error C2280 is reported saying that reference type data members are used? why?
This error message is really strange, how can I troubleshoot it?
template <typename Type>
class CVectorReference2 {
public:
Type& m_x, & m_y;
Type& m_s, & m_t;
CVectorReference2(Type& Value0, Type& Value1) :
m_x(Value0), m_y(Value1),
m_s(Value0), m_t(Value1) { }
virtual ~CVectorReference2() {}
CVectorReference2(const CVectorReference2<Type>& Vector) :
m_x(Vector.m_x), m_y(Vector.m_y),
m_s(Vector.m_s), m_t(Vector.m_t) { }
template<typename InputType> //<-------
CVectorReference2<Type>& operator=(const CVectorReference2<InputType>& Vector) {
Assign(Vector);
return *this;
}
template<typename InputType>
inline void Assign(const CVectorReference2<InputType>& Vector) {
m_x = (Type)Vector.m_x;
m_y = (Type)Vector.m_y;
}
};
int main() {
double e0, e1, e2 = 1.0, e3 = 2.0;
CVectorReference2<double> P0(e0, e1);
CVectorReference2<double> P1(e2, e3);
CVectorReference2<double> P2(P0);
P2 = P1;
}
Share
Improve this question
asked Mar 10 at 13:55
CROMACROMA
213 bronze badges
3
|
1 Answer
Reset to default 2The special member functions of the forms: copy constructor, copy assignment, move constructor, move assignment, and destructor are never function templates.
There is an implicit CVectorReference2<Type>& operator=(const CVectorReference2<Type>& Vector)
that is deleted because you have a reference member. You need to add that function like you did in code block number 2 so that the assignment operator is not deleted.
operator=
(in the last snippet) is not really a proper assignment operator, which is therefore by default deleted (due to the reference member). This is different in the 2nd and 1st snippets where you actually supply a proper assignment operator which is therefore no longer deleted. – wohlstad Commented Mar 10 at 14:03