Q:

Ambiguous overload for operator+

There is polynomial class in Boost math library: Boost polynomial class. I want to expand abilities of this class by adding new functions and I use inheritance as follows:
#ifndef POLY_HPP
#define POLY_HPP

#include <boost/math/tools/polynomial.hpp>

template <class T>
class Poly : public boost::math::tools::polynomial<T>{
public:

    Poly(const T* data, unsigned order) : boost::math::tools::polynomial<T>(data, order){

    }
};

#endif 

Now I declare two objects of this class and I want to add them:
    int a[3] = {2, 1, 3};
    Poly<int> poly(a, 2);
    int b[2] = {3, 1};
    Poly<int> poly2(b, 1);
    std::cout << (poly + poly2) << std::endl;

But there is an error during compliation:
main.cpp: In function ‘int main()’:
main.cpp:28:26: error: ambiguous overload for ‘operator+’ in ‘poly + poly2’
/usr/local/include/boost/math/tools/polynomial.hpp:280:22: note: candidates are: boost::math::tools::polynomial<T> boost::math::tools::operator+(const U&, const boost::math::tools::polynomial<T>&) [with U = Poly<int>, T = int]
/usr/local/include/boost/math/tools/polynomial.hpp:256:22: note:                 boost::math::tools::polynomial<T> boost::math::tools::operator+(const boost::math::tools::polynomial<T>&, const U&) [with T = int, U = Poly<int>]
/usr/local/include/boost/math/tools/polynomial.hpp:232:22: note:                 boost::math::tools::polynomial<T> boost::math::tools::operator+(const boost::math::tools::polynomial<T>&, const boost::math::tools::polynomial<T>&) [with T = int]
make[2]: Leaving

There are defined three overloaded functions for operator+. I thought it should take:
boost::math::tools::polynomial<T> boost::math::tools::operator+(const boost::math::tools::polynomial<T>&, const boost::math::tools::polynomial<T>&)

because Poly class is inherited from Boost polynomial and arguments pass the best, however it doesn't happen. How to add two Poly class objects without explicit new definition of operator+?

A:

This is not possible as far as I know, you'll need something like
template <class T>
Poly<T> operator + (const Poly<T>& a, const Poly<T>& b) {
    return Poly<T>(static_cast< boost::math::tools::polynomial<T> >(a) +
                   static_cast< boost::math::tools::polynomial<T> >(b));
}

to disambiguate the call (which needs an appropriate conversion constructor from boost::math::tools::polynomial<T> to Poly<T> in your class...)

