How does “using std::swap” enable ADL?

后端 未结 2 1420
时光说笑
时光说笑 2020-12-16 11:54

In What is the copy-and-swap idiom this example is shown:

friend void swap(dumb_array& first, dumb_array& second) // nothrow
{
    // enable ADL (not         


        
2条回答
  •  抹茶落季
    2020-12-16 12:33

    The "enable ADL" comment applies to the transformation of

    std::swap(first.mSize, second.mSize);
    std::swap(first.mArray, second.mArray);
    

    to

    using std::swap;
    swap(first.mSize, second.mSize);
    swap(first.mArray, second.mArray);
    

    You're right, ADL only requires an unqualified name, but this is how the code is re-worked to use an unqualified name.

    Just plain

    swap(first.mSize, second.mSize);
    swap(first.mArray, second.mArray);
    

    wouldn't work, because for many types, ADL won't find std::swap, and no other usable swap implementation is in scope.

提交回复
热议问题