Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中复制不同模板类型的对象+;_C++_Templates_Operator Overloading - Fatal编程技术网

C++ 如何在C+中复制不同模板类型的对象+;

C++ 如何在C+中复制不同模板类型的对象+;,c++,templates,operator-overloading,C++,Templates,Operator Overloading,如何使(与不同模板类型的对象)a*b和b*a给出相同的结果,根据通常的C++类型推广规则 > /p>确定结果的类型。 例如: int main() { number<float> A(2.0f); number<double> B(3.0); A*B; // I want 6.0 (double) B*A; // I want 6.0 (double) return 0; } intmain() { A号(2.0f);

如何使(与不同模板类型的对象)a*b和b*a给出相同的结果,<强>根据通常的C++类型推广规则<强> > /p>确定结果的类型。 例如:

int main()
{
    number<float> A(2.0f);    
    number<double> B(3.0);
    A*B; // I want 6.0 (double)
    B*A; // I want 6.0 (double)

    return 0;
}
intmain()
{
A号(2.0f);
数字B(3.0);
A*B;//我想要6.0(双精度)
B*A;//我想要6.0(双精度)
返回0;
}
目前,我只能将同一模板类型的对象相乘。例如,类似这样的内容:

template<typename T>
class number
{
    public:

        number(T v) : _value(v) {}

        T get_value() const { return _value; }

        number& operator*=(const number& rhs)
        {
            _value *= rhs.get_value(); 
            return *this;
        } 

    private:

        T _value;
};

template<typename T>
inline number<T> operator*(number<T> lhs, const number<T>& rhs)
{
  lhs *= rhs;
  return lhs;
}
模板
班号
{
公众:
数字(tv):_值(v){}
T get_value()常量{return_value;}
编号和运算符*=(常数编号和rhs)
{
_value*=rhs.get_value();
归还*这个;
} 
私人:
T_值;
};
样板
内联编号运算符*(编号lhs、常量编号和rhs)
{
lhs*=rhs;
返回lhs;
}
编辑:或者,与答案一样,我可以将不同模板类型的对象相乘,但始终返回与lhs相同的类型。有没有办法返回其类型由标准类型提升规则确定的对象


编辑2:如果可能的话,我希望避免使用C++11功能。

您需要为
操作符*()

模板
班号{
公众:
// ...
样板
编号和运算符*=(常数编号和rhs){
// ...
}
// ...
};
二元运算符也是如此

template<typename T,typename U>
inline number<T> operator*(number<T> lhs, const number<U>& rhs) {
  lhs *= rhs;
  return lhs;
}
模板
内联编号运算符*(编号lhs、常量编号和rhs){
lhs*=rhs;
返回lhs;
}

您需要为
操作符*()

模板
班号{
公众:
// ...
样板
编号和运算符*=(常数编号和rhs){
// ...
}
// ...
};
二元运算符也是如此

template<typename T,typename U>
inline number<T> operator*(number<T> lhs, const number<U>& rhs) {
  lhs *= rhs;
  return lhs;
}
模板
内联编号运算符*(编号lhs、常量编号和rhs){
lhs*=rhs;
返回lhs;
}

您必须模板化,例如
rhs
参数:

template<typename T>
class number
{
public:

    number(T v) : _value(v) {}

    T get_value() const { return _value; }
    template<class E>
    number& operator*=(const number<E>& rhs)
    {
        _value *= rhs.get_value(); 
        return *this;
    } 

private:

    T _value;
};

template<class T, class E, class RET = decltype(T()*E())>
number<RET> operator*(number<T>& lhs, const number<E>& rhs)
{
    return lhs.get_value()*rhs.get_value();
}
模板
班号
{
公众:
数字(tv):_值(v){}
T get_value()常量{return_value;}
样板
编号和运算符*=(常数编号和rhs)
{
_value*=rhs.get_value();
归还*这个;
} 
私人:
T_值;
};
样板
编号操作员*(编号和左侧、常量编号和右侧)
{
返回lhs.get_value()*rhs.get_value();
}

您必须模板化,例如
rhs
参数:

template<typename T>
class number
{
public:

    number(T v) : _value(v) {}

    T get_value() const { return _value; }
    template<class E>
    number& operator*=(const number<E>& rhs)
    {
        _value *= rhs.get_value(); 
        return *this;
    } 

private:

    T _value;
};

template<class T, class E, class RET = decltype(T()*E())>
number<RET> operator*(number<T>& lhs, const number<E>& rhs)
{
    return lhs.get_value()*rhs.get_value();
}
模板
班号
{
公众:
数字(tv):_值(v){}
T get_value()常量{return_value;}
样板
编号和运算符*=(常数编号和rhs)
{
_value*=rhs.get_value();
归还*这个;
} 
私人:
T_值;
};
样板
编号操作员*(编号和左侧、常量编号和右侧)
{
返回lhs.get_value()*rhs.get_value();
}
您可以使用获取退货所需的类型。比如说

template<typename X>
struct number
{
  // ...

  template<typename Y>
  number(number<Y> const&other);              // needed in line 1 below

  template<typename Y>
  number&operator=(number<Y> const&other);    // you may also want this

  template<typename Y>
  number&operator*=(number<Y> const&other);   // needed in line 2 below

  template<typename Y>
  number<typename std::common_type<X,Y>::type> operator*(number<Y> const&y) const
  {
    number<typename std::common_type<X,Y>::type> result=x;   // 1
    return result*=y;                                        // 2
  }
};
模板
结构编号
{
// ...
样板
number(number const&other);//需要在下面的第1行中输入
样板
number&operator=(number const&other);//您可能还需要这个
样板
数字和运算符*=(数字常量和其他);//在下面的第2行中需要
样板
数字运算符*(数字常量和y)常量
{
数字结果=x;//1
返回结果*=y;//2
}
};
我省略了模板化构造函数和
操作符*=
的实现


不幸的是,
std::common_type
是C++11,出于模糊的原因,您希望避免使用它。如果只使用内置类型(
double
float
int
等),则可以轻松实现自己版本的
common\u type
。但是,如果您想进行复杂的元模板编程,强烈建议您继续使用C++11–它已经使用了4年,并且基本上是向后兼容的。

您可以使用它来获取返回所需的类型。比如说

template<typename X>
struct number
{
  // ...

  template<typename Y>
  number(number<Y> const&other);              // needed in line 1 below

  template<typename Y>
  number&operator=(number<Y> const&other);    // you may also want this

  template<typename Y>
  number&operator*=(number<Y> const&other);   // needed in line 2 below

  template<typename Y>
  number<typename std::common_type<X,Y>::type> operator*(number<Y> const&y) const
  {
    number<typename std::common_type<X,Y>::type> result=x;   // 1
    return result*=y;                                        // 2
  }
};
模板
结构编号
{
// ...
样板
number(number const&other);//需要在下面的第1行中输入
样板
number&operator=(number const&other);//您可能还需要这个
样板
数字和运算符*=(数字常量和其他);//在下面的第2行中需要
样板
数字运算符*(数字常量和y)常量
{
数字结果=x;//1
返回结果*=y;//2
}
};
我省略了模板化构造函数和
操作符*=
的实现



不幸的是,
std::common_type
是C++11,出于模糊的原因,您希望避免使用它。如果只使用内置类型(
double
float
int
等),则可以轻松实现自己版本的
common\u type
。但是,如果您想进行复杂的元模板编程,强烈建议您继续使用C++11,因为它已经使用了4年,并且基本上是向后兼容的。

仅此一点是不够的,您还必须为重载的
*
-operator@MikeMB这正是我的建议?不仅是一元
数字和运算符*=(常量数字和rhs)
,还有二进制
数字运算符*(数字lhs、常量数字和rhs)
但它总是返回类型T,而从不返回类型U。我真正感兴趣的部分是如何正确地获取返回类型。例如,我想要2.0f*3.0==3.0*2.0f==2.0*3.0f==3.0f*2.0==6.0(双精度)。这是可能的吗?如果您想返回
U
,只需在返回类型声明中切换它就行了?这还不够,您还必须为重载的
*
引入第二个模板参数-operator@MikeMB这正是我的建议?不仅仅是一元
数和运算符*=(常量数和rhs)
还有二进制
数字运算符*(numb