相关疑难解决方法(0)

什么`在类方法实现的主体内使用std :: swap`意味着什么?

在一些c ++实践中,我试图学习并采用复制交换习语,对这个问题进行彻底的解释:复制交换习语.

但我发现了一些我从未见过的代码:using std::swap; // allow ADL在这个例子中

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
    }
};
Run Code Online (Sandbox Code Playgroud)
  1. using std::swap;在函数实现的主体内部意味着什么?
  2. ADL是什么意思?

c++ stl using std argument-dependent-lookup

26
推荐指数
2
解决办法
5310
查看次数

"使用std :: swap"如何启用ADL?

什么是复制和交换习语这个例子显示:

friend void swap(dumb_array& first, dumb_array& second) // nothrow
{
    // enable ADL (not necessary in our case, but good practice)
    using std::swap; 

    // by swapping the members of two classes,
    // the two classes are effectively swapped
    swap(first.mSize, second.mSize); 
    swap(first.mArray, second.mArray);
}
Run Code Online (Sandbox Code Playgroud)

如何using std::swap启用ADL?ADL只需要一个不合格的名称.我看到的唯一好处using std::swap是,因为std::swap是一个函数模板,你可以在call(swap<int, int>(..))中使用模板参数列表.

如果不是这样的话,那是using std::swap为了什么?

c++ argument-dependent-lookup c++11

14
推荐指数
2
解决办法
3076
查看次数

标签 统计

argument-dependent-lookup ×2

c++ ×2

c++11 ×1

std ×1

stl ×1

using ×1