Create templates overloading friend operators in t

2019-07-18 11:43发布

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
}

2条回答
狗以群分
2楼-- · 2019-07-18 12:36

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
查看更多
Lonely孤独者°
3楼-- · 2019-07-18 12:39

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

查看更多
登录 后发表回答