[英]Assignment operator overloading with a C++ class template
I have a C++ class template for representing real- and complex valued 2D fields.我有一个 C++ class 模板,用于表示实值和复值二维字段。 I'd like to overload the assignment operator to achieve deep-copying the data from one field to another.我想重载赋值运算符以实现将数据从一个字段深度复制到另一个字段。 For now, I've restricted the data to either double
or std::complex<double>
.现在,我将数据限制为double
或std::complex<double>
。 This means there are 4 different cases to consider: double
-to- double
, double
-to- std::complex<double>
, std::complex<double>
-to- double
and std::complex<double>
-to- std::complex<double>
.这意味着需要考虑 4 种不同的情况: double
到double
、 double
到std::complex<double>
、 std::complex<double>
到double
和std::complex<double>
到- std::complex<double>
。 I want to handle the std::complex<double>
-to- double
case by taking the real part of the complex value;我想通过取复值的实部来处理std::complex<double>
-to- double
的情况; for the other cases it's just a trivial assignment.对于其他情况,这只是一项微不足道的任务。 I'm however struggling to get the code to compile.然而,我正在努力让代码编译。 The following is a simple mock version that captures the issues:以下是捕获问题的简单模拟版本:
#include <complex>
template<class T>
class Number {
private:
// should make Number<T>.m_value visible to Number<U>
template<class U>
friend class Number;
T m_value;
public:
Number(const T value) : m_value{value} {
// restricting the data
static_assert(
std::is_same<T, double>::value || std::is_same<T, std::complex<double>>::value,
"Error: Number::Number: Only 'double' and 'std::complex<double>' are supported currently!"
);
// no copying allowed
Number(const Number& orig) = delete;
};
// general case
template<class U>
Number<T>& operator=(const Number<U>& another) {
m_value = another.m_value;
return *this;
}
};
// specialization
template<> Number<double>& Number<double>::operator=(const Number<std::complex<double>>& another) {
m_value = std::real(another.m_value);
}
int main(int argc, char** argv) {
const std::complex<double> I{0.0, 1.0};
Number<double> n{0.0};
Number<std::complex<double>> m{1.0 + I*2.0};
n = m;
return 0;
}
Compiler output:编译器 output:
g++ -c -g -std=c++14 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
main.cpp:28:28: error: template-id ‘operator=<>’ for ‘Number<double>& Number<double>::operator=(const Number<std::complex<double> >&)’ does not match any template declaration
template<> Number<double>& Number<double>::operator=(const Number<std::complex<double>>& another) {
^
main.cpp:28:97: note: saw 1 ‘template<>’, need 2 for specializing a member function template
template<> Number<double>& Number<double>::operator=(const Number<std::complex<double>>& another) {
^
main.cpp: In instantiation of ‘Number<T>& Number<T>::operator=(const Number<U>&) [with U = std::complex<double>; T = double]’:
main.cpp:36:5: required from here
main.cpp:23:15: error: cannot convert ‘const std::complex<double>’ to ‘double’ in assignment
m_value = another.m_value;
^
I can't seem to figure out how to implement the assignment overloads.我似乎无法弄清楚如何实现赋值重载。 I have tried to find a solution for my problem (eg from "Similar questions") and have come across many helpful questions and answers, and have incorporated many things from them to my code.我试图为我的问题找到解决方案(例如,来自“类似问题”)并且遇到了许多有用的问题和答案,并将其中的许多内容合并到我的代码中。 I however haven't found a solution to my specific problem and seem to be stuck.然而,我还没有找到解决我的具体问题的方法,而且似乎被卡住了。 Any suggestions?有什么建议么? Thanks!谢谢!
It actually says what it expects in the line:它实际上说了它在行中的期望:
main.cpp:28:97: note: saw 1 ‘template<>’, need 2 for specializing a member function template
You need one 'template<>' to specialize T
in class template and one more to specialize U
in member function template:你需要一个'template<>'来专门化 class 模板中的T
,再需要一个来专门化成员 function 模板中的U
:
template<>
template<>
Number<double>& Number<double>::operator=(const Number<std::complex<double>>& another) { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.