[英]Ambiguous Class Template Conversion
How would one add a template constructor to the class so that copy initialization from complex to complex is performed explicitly and without ambiguity?如何将模板构造函数添加到类中,以便明确执行从复杂到复杂的复制初始化并且没有歧义? Is there a solution that is compiler and C++ version/standard agnostic?
是否有编译器和 C++ 版本/标准不可知的解决方案? Is there an approach that only requires definition of a constructor without an additional operator overload?
是否有一种方法只需要定义构造函数而无需额外的运算符重载?
I included the template copy constructor and operator overload (last two methods defined in the class) but the compiler gives me the following message.我包括了模板复制构造函数和运算符重载(类中定义的最后两个方法),但编译器给了我以下消息。
Compilation error
main.cpp: In function ‘void testTemplateConstructor()’:
main.cpp:74:27: error: conversion from ‘complex<float>’ to ‘complex<double>’ is ambiguous
complex<double> cd = cf;
^~
main.cpp:35:5: note: candidate: complex<T>::operator complex<X>() [with X = double; T = float]
operator complex<X>(){
^~~~~~~~
main.cpp:29:5: note: candidate: complex<T>::complex(complex<X>&) [with X = float; T = double]
complex(complex<X>& arg) {
^~~~~~~
This is the test case being utilized.这是正在使用的测试用例。
void testTemplateConstructor() {
complex<float> cf{1.0f, 2.0f};
complex<double> cd = cf;
Assert(cf.real()==cd.real(), "Real parts are different.");
Assert(cf.imag()==cd.imag(), "Imaginary parts are different.");
}
template <typename T> class complex{
private:
typedef complex<T> complexi;
T real_;
T imag_;
public:
complex(){
real_ = 0;
imag_ = 0;
}
complex(T a, T b){
real_ = a;
imag_ = b;
}
complex(T a){
real_ = a;
}
complex(complex<T>& comp){
real_ = comp.real_;
imag_ = comp.imag_;
}
template <typename X>
complex(complex<X>& arg) {
real_ = arg.real_;
imag_ = arg.imag_;
}
template <typename X>
operator complex<X>(){
return complex<T>();
}
};
so that copy initialization from complex to complex is performed explicitly and without ambiguity?
以便从复杂到复杂的复制初始化显式且没有歧义地执行? Is there a solution that is compiler and C++ version/standard agnostic?
是否有编译器和 C++ 版本/标准不可知的解决方案?
Yes, in this particular example, you can add a low level const
to the parameter of the ctors.是的,在这个特定的示例中,您可以将低级
const
添加到 ctor 的参数中。
template <typename T> class complex{
public:
typedef complex<T> complexi;
T real_;
T imag_;
public:
complex(){
real_ = 0;
imag_ = 0;
}
complex(T a, T b){
real_ = a;
imag_ = b;
}
complex(T a){
real_ = a;
}
complex(const complex<T>& comp){
std::cout<<"nornal version";;
real_ = comp.real_;
imag_ = comp.imag_;
}
template <typename X>
complex(const complex<X>& arg) {
std::cout<<"template version";;
real_ = arg.real_;
imag_ = arg.imag_;
}
};
void testTemplateConstructor() {
complex<float> cf{1.0f, 2.0f};
complex<double> cd = cf;
}
This is the solution I was looking for, this works with the C++11 standard and compiler versions as old as x86_64 gcc 4.7.1 and clang 3.4.1.这是我一直在寻找的解决方案,它适用于 C++11 标准和编译器版本,如 x86_64 gcc 4.7.1 和 clang 3.4.1。 The only difference apart from the use of static_cast is the use of getters.
除了使用 static_cast 之外,唯一的区别是使用 getter。
using namespace std;
template <typename T> class complex{
public:
typedef complex<T> complexi;
T real_;
T imag_;
public:
complex(){
real_ = 0;
imag_ = 0;
}
complex(T a, T b){
real_ = a;
imag_ = b;
}
complex(T a){
real_ = a;
}
complex(const complex<T>& comp){
real_ = comp.real_;
imag_ = comp.imag_;
}
template <typename X>
complex(const complex<X>& rhs){
real_ = static_cast<T>(rhs.real());
imag_ = static_cast<T>(rhs.imag());
}
T real() const {
return real_;
}
T imag() const {
return imag_;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.