Overloading a class statement wrapped in smart pointers

I am trying to do some operator overloading (* and +) in a class that is wrapped in smart pointers.

auto p = std::make_unique<Polynomial<T>>(); 

When I try to overload it using regular overloading, it explicitly requests smart pointer types.

Change, therefore:

 std::unique_ptr<Polynomial<T>> operator+(const std::unique_ptr<Polynomial<T>>& right); template<class T>std::unique_ptr<Polynomial<T>> Polynomial<T>::operator+(const std::unique_ptr<Polynomial<T>>& right) { //Do stuff } 

And the error:

Error from trying to use the + operator

So, how do you overload normal operators when a class is encapsulated in smartpointer?

+5
source share
3 answers

Not necessary.

Run these operations with pointers, not pointers:

 *p = *p + *p 

This would be very confusing for users of your code if the sudden pointers had completely different and unexpected semantics.

This also confuses you.

+11
source

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:

enter image description here

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.

+2
source

For a direct answer to your question, I agree with @LightnessRacesinOrbit: Just not . But the reason for writing another answer is because the question you ask sounds a bit like the XY-problem to me.

Do you have a Polynomial<T> class, but do you expect the user to wrap it with a smart pointer? This is logical, since a reasonable design would simply expect the user to work with Polynomial directly, therefore, providing normal operators that work with this type directly.

Take a step back and think about why you think the smart pointer will be used / needed / useful. Often this is done for alleged reasons for effectiveness, right? But that just means you have to rethink how you implemented Polynomial . Take a look at the following:

  • Does the Polynomial move-constructor and -adaptation have? If not, do it.
  • What about stored data? Perhaps internal storage should be controlled by a smart pointer. Together with move-semantics, this can lead to very efficient operations.
  • To overload the operator, implement a+=b; , use the library to create a+b for you. Check out Boost.Operators or my df.operators , the latter is also migration aware.

With proper implementation, the desire to use std::unique_ptr<Polynomial<T>> should disappear :)

+2
source

All Articles