Reputation: 929
Consider a struct
template<typename T, size_t N>
struct Something {
std::array<T,N> args;
// Some constructors
};
Now let's overload =
operator for Something<T>
. In fact I can implement it two ways.
First Way
Something& operator=(const Something& rhs){
// an implementation with copy semantics
}
Something& operator=(Something&& rhs) {
// an implementation with move semantics
}
Second Way
Something& operator=(Something rhs){
// implement with move semantics
}
So, my question is what is the most standard way and the most optimal way to do overloading First Way or Second Way?
Upvotes: 0
Views: 108
Reputation: 122133
For this particular case you should not implement the assignment operator. The compiler does that already for you:
#include <array>
#include <cstddef>
template<typename T, size_t N>
struct Something {
std::array<T,N> args;
};
int main() {
Something<int,42> a;
Something<int,42> b;
a = b;
}
For the general case I refer you to What are the basic rules and idioms for operator overloading?. And consider that not everything can be moved and not everything can be copied. Moreover, sometimes a move is just a copy. Hence, it depends.
Upvotes: 2