繁体   English   中英

模糊类模板转换

[英]Ambiguous Class Template Conversion

如何将模板构造函数添加到类中,以便明确执行从复杂到复杂的复制初始化并且没有歧义? 是否有编译器和 C++ 版本/标准不可知的解决方案? 是否有一种方法只需要定义构造函数而无需额外的运算符重载?

我包括了模板复制构造函数和运算符重载(类中定义的最后两个方法),但编译器给了我以下消息。

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) {
     ^~~~~~~

这是正在使用的测试用例。

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>();
    }
};

以便从复杂到复杂的复制初始化显式且没有歧义地执行? 是否有编译器和 C++ 版本/标准不可知的解决方案?

是的,在这个特定的示例中,您可以将低级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;
    
    
}

演示

这是我一直在寻找的解决方案,它适用于 C++11 标准和编译器版本,如 x86_64 gcc 4.7.1 和 clang 3.4.1。 除了使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM