Say I have a polynomial class where the degree is controlled by a template, such as the following:
template<int degree>
class Polynomial {
....
}
How can I create an Add function that adds two polynomials of potentially different degrees? Ideally it would be something like
template<int degree1, int degree2>
Polynomial<max(degree1, degree2)> Add(Polynomial<degree1> poly1, Polynomial<degree2> poly2)
{
...
}
Is there a way to do this in c++?
In C++11 you can use a
constexprfunction for this:Whereas in pre-C++11, there is no concept of
constexprfunctions, but the ternary conditional operator can still be used directly: