Stephane Rolland Stephane Rolland - 4 months ago 7
C++ Question

what does `using std::swap` inside the body of a class method implementation mean?

I was doing some c++ practicing this afternoon, and was trying to learn and adopt the copy-swap idiom following this thoroughfully explanation on this question: the Copy-Swap Idiom.

But I found some code I had never seen:

using std::swap; // allow ADL
in this example

class dumb_array
{
public:
// ...

void swap(dumb_array& pOther) // nothrow
{
using std::swap; // allow ADL /* <===== THE LINE I DONT UNDERSTAND */

swap(mSize, pOther.mSize); // with the internal members swapped,
swap(mArray, pOther.mArray); // *this and pOther are effectively swapped
}
};



  1. what does
    using std::swap;
    mean
    inside the body of a function
    implementation ?

  2. what does ADL mean ?


Answer

The using keyword has scoped effect.

This means that std::swap can be referred to as swap during the scope of the using keyword.