在模板类中创建模板重载运算符

Create templates overloading operator in template class

本文关键字:建模 重载 创建 运算符      更新时间:2023-10-16

昨天我在这里看到一个问题链接

它有答案,但它不适用于我。

因此,正如第一个问题

 NSize<30, unsigned char, unsigned short> a(101);
 NSize<25, unsigned char, unsigned short> b(120);
 NSize<15, unsigned char, unsigned short> c(115);
 NSize<30> res = (a*b)*(a*c);

和模板类

template<int size,typename basic_type=unsigned int,typename long_type=unsigned long long,long_type base=256>
class NSize
{
   ...
}

Lol4t0 的解决方案

template <int lengthA, int lengthB>
NSize<lengthA + lengthB>  operator * (const NSize<lengthA> &a, const NSize<lengthB> &b)
{
    return NSize<lengthA + lengthB>(...);
}

所以,我们有非常大的数字。对于它们,我们有我们的类NSize,其中size是我们可以为这个数字存储的位数。

因此,对于NSize<3>,我们可以存储的最大数字是999。

我们希望NSize的一些运算符过载。当size相似时很容易,但当size不同时,我们会遇到问题。

Lol4t0's解决方案对我不起作用,这就是CLion给我的

error: 'NSize<max(aS, bS)> NSize<size, basic_type, long_type, base>::operator*(NSize<aS>&, NSize<bS>&)' must take either zero or one argument
     NSize<max(aS,bS)> operator * (NSize<aS> &a, NSize<bS> &b)
error: no match for 'operator*' (operand types are 'NSize<30, unsigned char, short unsigned int>' and 'NSize<30, unsigned char, short unsigned int>')
     NSize<30, unsigned char, unsigned short> resc = (a*b)*(a*c);

有什么建议吗?

如果没有一个可编译的例子,要想解决这个问题并不容易,但对我来说,问题似乎如下:重载乘法运算符仅为默认模板参数定义。也就是说,代码

template <int lengthA, int lengthB>
NSize<lengthA + lengthB>  operator * (const NSize<lengthA> &a, const NSize<lengthB> &b)
{
    return NSize<lengthA + lengthB>(...);
}

对应

template <int lengthA, int lengthB>
NSize<lengthA + lengthB, unsigned int, unsigned long long, 256>  operator * 
(const NSize<lengthA, unsigned int, unsigned long long, 256> &a, const NSize<lengthB, unsigned int, unsigned long long, 256> &b)
{
    return NSize<lengthA + lengthB, unsigned int, unsigned long long, 256>(...);
}

但接下来,你用两类类型的来称呼它

NSize<some_integer, unsigned char,       unsigned short>
                  //^^^^^^^^^^^^         ^^^^^^^^^^^^^^
                  //not: unsigned int    not: unsigned long long

解决方案很简单:在operator*定义中拼写出所有相关的模板参数,从而对返回的参数类型进行详细控制。

如果你发现自己一直在这样做,你可能更喜欢使用类似std::common_type的帮助类

template<typename T, typename S>
struct common_type
{
    using type = typename std::common_type<T, S>::type;
};
template<int size1, typename basic_type1 /*, ... */
       , int size2, typename basic_type2 /*, .. */>
struct common_type<NSize<size1, basic_type1 /*, ... */>
                 , NSize<size2, basic_type2 /*, ... */> >
{
    using type = NSize<size1+size2, typename std::common_type<basic_type1,basic_type2>::type /*, ... */ >;
};

并将其作为乘法运算的结果返回:

template<typename ... Args1, typename ... Args2>
auto operator*(Nsize<Args1 ...> const& nsize1, Nsize<Args2 ...> const& nsize2)
    -> typename common_type<Nsize<Args1 ...>, Nsize<Args2 ...> >::type
{
    //...
}