Suppose that I have following class
template<unsigned char B, unsigned char F>
class Foo
{
.....
}
I hope to overload the operator+ so that if the two inputs are of
Foo<B1, F1>
and
Foo<B2, F2>,
respectively, I would like the return value is type of
Foo<max(B1, B2), max(F1, F2)>.
or something like
Foo<max(B1-F1, B2-F2)+max(F1, F2), max(F1, F2)>.
Any tips?
Just compute your new type in the operator+ return type, and use MPL for comparisons
Moreover, you don’t need friend for that neither your operator+ need to be mutable.
Simple example with simple classes:
Now, notice how it is cumbersome. An usual idiom in this case is to ue Boost MPL Integral Types instead of raw value whenever possible
EDIT:
Also, this makes the return type of + a bit complex to write down in pre_C++11 auto.
Another classical stuff is to make this into a function object following the result_of protocol so you have a meta-function handy to compute the return type in an opaque way.