在模板类中创建模板重载运算符
Create templates overloading operator in template class
昨天我在这里看到一个问题链接
它有答案,但它不适用于我。
因此,正如第一个问题
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
{
//...
}
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 重载==不适用于二进制树
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 正在尝试重载二进制搜索树分配运算符
- 重载Singly Linked List中的赋值运算符
- 取消引用运算符不能重载