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
}
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 theoperator*
, like so:instead of
You can implement your
operator*
the following way:The idea is that we should define
opeator *
the way it could take 2Nsize
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
will end up with
res
size being 285Live example
If you want to stick to the bigger argument size you can replace sum with maximum:
Live example