0

I have the following loop (where: Array<T> elements;):

while (lengths[tempElement] == START) {
    T next = elements[tempElement];
    elements[tempElement] = elementToFind;
    tempElement = next;
}

I would like to use the following swap function:

void swap(T& firstElement, T& secondElement) {
    T temp = firstElement;
    firstElement = secondElement;
    secondElement = temp;
}

In order to shorten the code. So I used:

swap(elements[tempElement],elementToFind);

But I think it does not do what I expect. How can I use that swap function?

vesii
  • 2,276
  • 2
  • 15
  • 44

1 Answers1

0

Your desired code uses T &operator=(T const&) while std::swap uses T &operator=(T&&).

If you want it to switch it to use the copy constructor, you can overload std::swap as shown here.

Kostas
  • 3,955
  • 12
  • 30