Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was trying to learn and adopt the copy-swap idiom following this thorough 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 ?
like image 828
Stephane Rolland Avatar asked Jan 24 '11 13:01

Stephane Rolland


People also ask

What does std :: swap do?

The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables. Parameters: The function accepts two mandatory parameters a and b which are to be swapped. The parameters can be of any data type.

How do you swap classes in C++?

Create a class Swap, declare three variables in it, i.e., a, b, and temp and create a constructor for inputs. Declare a friend function in it. Define the friend function outside the class scope by taking arguments as call by reference to pass the copy of Swap Object. Perform the swap operation with Swap variables.

Why do we use std in C++?

So they created a namespace, std to contain this change. The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them.

Why is using namespace std considered bad practice?

While this practice is okay for example code, pulling in the entire std namespace into the global namespace is not good as it defeats the purpose of namespaces and can lead to name collisions. This situation is called namespace pollution.


2 Answers

This mechanism is normally used in templated code, i.e. template <typename Value> class Foo.

Now the question is which swap to use. std::swap<Value> will work, but it might not be ideal. There's a good chance that there's a better overload of swap for type Value, but in which namespace would that be? It's almost certainly not in std:: (since that's illegal), but quite likely in the namespace of Value. Likely, but far from certain.

In that case, swap(myValue, anotherValue) will get you the "best" swap possible. Argument Dependent Lookup will find any swap in the namespace where Value came from. Otherwise the using directive kicks in, and std::swap<Value> will be instantiated and used.

In your code, mSize is likely an integral type, and mArray a pointer. Neither has an associated namespace, and std::swap is with 99.9% certainty optimal for them anyway. Therefore, the using std::swap; declaration seems useless here.

like image 192
MSalters Avatar answered Sep 26 '22 15:09

MSalters


The using keyword has scoped effect.

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

like image 43
Benoit Avatar answered Sep 24 '22 15:09

Benoit