c++ - 将原始数据类型解释为用户类

标签 c++ class templates

TLDR;我想使用为我的类定义的具有原始数据类型的运算符,这些数据类型可以转换为我的类而无需类型转换/初始化新变量。即,

mycomplex x = 5 , y ;
y = x + 3 ;

作为一个副项目,我正在开发一个复数类。出于显而易见的原因,任何原始数字数据类型都可以解释为复数。因此,在涉及原始类型和复数的任何操作中,我希望允许原始类型转换为复数,并使用我定义的涉及两个复数的操作继续操作。

每个基本类型都可以在每个运算符上重载,但顺序很重要,并且有了 4 个基本运算符和一些高级数学函数,它很快就会变成大量代码。所以问题是,如何编写我的类,以便将基本类型“转换”为复杂类型并执行正常的复杂操作。

如果 main 的倒数第二行被注释掉,则此代码可以编译。此处的目标是让该行评估为 cout 为 8+2i 的 mycomplex。

#include <iostream>

template <class T>
class mycomplex{
    private:
        T real ; 
        T imag ; 
    public:
        mycomplex( const mycomplex<T> & x ) ;
        mycomplex( const T & realx , const T & imagx ) ; 
        mycomplex( const T & x ) ; 
        mycomplex( ) ;

        template <class U>
        friend std::ostream & operator << ( std::ostream & os , const mycomplex<U> & x ) ;

        template <class U>
        friend mycomplex<U> operator + ( const mycomplex<U> & lhs , const mycomplex<U> & rhs ) ;
} ;

int main( int argc , char * argv[] ){
    mycomplex<float> x = 5 , y( 3 , 2 ) ; 
    mycomplex<float> z = y + x ; 
    std::cout << x << '\n' << y << '\n' << z << std::endl ;
    z = 5 + y ; 
    return 0 ;
}

template <class T>
mycomplex<T>::mycomplex( const mycomplex<T> & x ){
    real = x.real ;
    imag = x.imag ; 
}
template <class T>
mycomplex<T>::mycomplex( const T & realx , const T & imagx ){
    real = realx ; 
    imag = imagx ; 
}
template <class T>
mycomplex<T>::mycomplex( const T & x ){
    real = x ; 
    imag = 0 ;
}
template <class T>
mycomplex<T>::mycomplex( ){
    real = 0 ; 
    imag = 0 ; 
}
template <class T>
std::ostream & operator << ( std::ostream & os , const mycomplex<T> & x ){
    os << x.real ; 
    if( x.imag >= 0 ) 
        os << "+" ;
    os << x.imag << "i" ;
    return os ;
}
template <class T>
mycomplex<T> operator + ( const mycomplex<T> & lhs , const mycomplex<T> & rhs ){
    mycomplex<T> ans ;
    ans.real = lhs.real + rhs.real ; 
    ans.imag = lhs.imag + rhs.imag ; 
    return ans ; 
}

我看了this answer并如您在上面看到的那样实现了它,但这并没有让这个编译,尽管它在您声明变量时很方便。我也看了this answer并向模板类型添加了类型转换,但这导致我的类转到模板,这与目标完全相反。它允许上面的代码编译,但是任何 mycomplex + T 都会给出没有虚部的 mycomplex,这是不正确的。

最佳答案

你可以使用 std::enable_if 但它非常丑陋。

我很匆忙,所以不幸的是我没有对此进行广泛测试或启用促销(float + complex<int> 给你 complex<float> )。 可运行代码 here .

#include <iostream>
#include <type_traits>

template <class T>
class mycomplex{
    private:
        T r ; 
        T i ; 
    public:
        using Underlying = T;
        mycomplex( const mycomplex<T> & x ) ;
        mycomplex( const T & realx , const T & imagx ) ; 
        mycomplex( const T & x ) ; 
        mycomplex( ) ;

        T real() const{
            return r;
        }
        T imag() const {
            return i;
        }

        template <class U>
        friend std::ostream & operator << ( std::ostream & os , const mycomplex<U> & x ) ;
} ;

template <class T>
mycomplex<T>::mycomplex( const mycomplex<T> & x ){
    r = x.r ;
    i = x.i ; 
}
template <class T>
mycomplex<T>::mycomplex( const T & realx , const T & imagx ){
    r = realx ; 
    i = imagx ; 
}
template <class T>
mycomplex<T>::mycomplex( const T & x ){
    r = x ; 
    i = 0 ;
}
template <class T>
mycomplex<T>::mycomplex( ){
    r = 0 ; 
    i = 0 ; 
}
template <class T>
std::ostream & operator << ( std::ostream & os , const mycomplex<T> & x ){
    os << x.r ; 
    if( x.i >= 0 ) 
        os << "+" ;
    os << x.i << "i" ;
    return os ;
}

template<typename T>
struct is_mycomplex : std::false_type{
};

template<typename T>
struct is_mycomplex<mycomplex<T>> : std::true_type{
};

static_assert(!is_mycomplex<int>::value);
static_assert(is_mycomplex<mycomplex<int>>::value);

template <typename T>
auto type_helper(){
    if constexpr (is_mycomplex<T>::value){
        return typename T::Underlying{};
    } else {
    return T{};
    }
}
template <class L, class R, typename = std::enable_if_t<
    is_mycomplex<L>::value + is_mycomplex<R>::value == 2
        ||
    (is_mycomplex<L>::value + is_mycomplex<R>::value == 1
     &&
     std::is_arithmetic_v<L> + std::is_arithmetic_v<R> ==1)
    >>
auto operator + ( const L& lhs , const R& rhs ){    
    using T = decltype(type_helper<L>());
    T real = 0; 
    T imag = 0;
    if constexpr(std::is_arithmetic_v<L>) {
        real+=lhs;
    } else {
        real+=lhs.real();
        imag+=lhs.imag();
    }
    if constexpr(std::is_arithmetic_v<R>) {
        real+=rhs;
    } else {
        real+=rhs.real();
        imag+=rhs.imag();
    }

    return mycomplex<T>(real, imag);
 ; 
}

int main(){
    mycomplex<float> x = 5 , y( 3 , 2 ) ; 
    mycomplex<float> z = y + x ; 
    std::cout << x << '\n' << y << '\n' << z << std::endl ;
    z = 5.5f + y ; 
    std::cout << "\n\n" << z << std::endl ;
}

关于c++ - 将原始数据类型解释为用户类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53233637/

相关文章:

c++ - 使用 c++0x - 安全吗?

Python:通过实例对象调用方法: "missing 1 required positional argument: ' self'”

具有原始数据类型的 Java 模板作为 C++ 模板

angular - 检查错误计数 Angular react 形式

c++ - 使用 libtool 从共享库加载重复的函数名

c++ - 模块化算法和 NTT(有限域 DFT)优化

c++ - 模板特化和实例化

c++ - 错误 : expected '=' , ','、 ';'、 'asm' 或 '__attribute__' token 之前的 ':'

c++ - 在 g++ 上无效使用不完整类型

templates - 将变量从父模板传递到 meteor 中的子模板