So, how do you overload normal operators when a class is encapsulated in smartpointer?
Just declare them as usual:
template<typename T> Polynomial<T> operator*(Polynomial<T> lhs, Polynomial<T> rhs) { ... } template<typename T> Polynomial<T> operator+(Polynomial<T> lhs, Polynomial<T> rhs) { ... }
and then enter something like:
auto p = std::make_unique<Polynomial<int>>(); auto q = std::make_unique<Polynomial<int>>();
just call:
auto x = (*p) * (*q); auto y = (*q) + (*p);
Live demo
The error you provided is:

due to the fact that your operator is overloaded:
std::unique_ptr<Polynomial<T>> operator+(const std::unique_ptr<Polynomial<T>>& right);
overloads the unary operator+ for expressions of type +x .
And other:
template<class T> std::unique_ptr<Polynomial<T>> Polynomial<T>::operator+(const std::unique_ptr<Polynomial<T>>& right) { //Do stuff }
overloads operator+ for operands of type Polynomial<T> and std::unique_ptr<Polynomial<T>> .
Therefore, two unique_ptr<Polynomial<T>> , namely x and y , the following expression:
x + y
cannot find overload resolution for operator+ .
I also strongly discourage operator overloading for standard library types.
source share