0

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?

Hrant Nurijanyan
  • 789
  • 2
  • 9
  • 26

1 Answers1

2

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;
}

Demo

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185