Create templates overloading friend operators in template class

点点圈 提交于 2019-12-24 00:36:26

问题


I really don't know how to create this properly. I have templates class with overloads for operators. And as i know i need to create templates to overload this operators for more universal form. My problem is that code not compile and i don't know how to fix it

Example:

    NSize<30> a(101);
    NSize<25> b(120);
    NSize<30> c(115);
    NSize<30> res = (a*b)*(a*c)*(c*b)*(b*a)*(a*c);

In the end, I should be able to do it. For now i defined it as :

template <int length>
friend NSize  operator * (const NSize &a, const NSize &b){
   //sth
}

And definition of my class looks like this:

template<int size,typename basic_type=unsigned int,typename long_type=unsigned long long,long_type base=256>
class NSize
{
   public:
   //sth
}

回答1:


You can implement your operator* the following way:

template<int size,typename basic_type=unsigned int,typename long_type=unsigned long long,long_type base=256>
class NSize
{
    // Note: that is not member but just friend
    template <int lengthA, int lengthB>
    friend NSize<lengthA + lengthB>  operator * (const NSize<lengthA> &a, const NSize<lengthB> &b);
};

// Actual definition that will work for every pair of Nsize of arbitrary size
template <int lengthA, int lengthB>
NSize<lengthA + lengthB>  operator * (const NSize<lengthA> &a, const NSize<lengthB> &b)
{
    return NSize<lengthA + lengthB>(...);
}

The idea is that we should define opeator * the way it could take 2 Nsize of arbitrary size. Then it generates the result with the size being the sum of argument sizes (to be sure result always fits)

However using this approach this code

int main() {
    NSize<30> a(101);
    NSize<25> b(120);
    NSize<30> c(115);
    auto res = (a*b)*(a*c)*(c*b)*(b*a)*(a*c);
}

will end up with res size being 285

Live example

If you want to stick to the bigger argument size you can replace sum with maximum:

constexpr int constmax(int a, int b) {
    return a>b?a:b;
}

//.....

friend NSize<constmax(lengthA,lengthB)>  operator * (const NSize<lengthA> &a, const NSize<lengthB> &b);

Live example




回答2:


Make operator* non-template, because why should it be a template.

Otherwise you would have to explicitly specify the undeducible template parameter length every time you want to call the operator*, like so:

a.operator*<length>(b)

instead of

a * b


来源:https://stackoverflow.com/questions/33487463/create-templates-overloading-friend-operators-in-template-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!