如何“使用std :: swap”启用ADL? [英] How does "using std::swap" enable ADL?

查看:83
本文介绍了如何“使用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);
}

使用std :: swap的精确度如何启用ADL? ADL只需要一个不标准的名称。我看到的使用std :: swap 的唯一好处是,因为 std :: swap 是一个函数模板,您可以在调用中使用模板参数列表( swap< int,int>(..))。

How exactly does using std::swap enable ADL? ADL only requires an unqualified name. The only benefits I see for using std::swap is that since std::swap is a function template you can use a template argument list in the call (swap<int, int>(..)).

如果不是这种情况,那么使用std :: swap 的目的是什么?

If that is not the case then what is using std::swap for?

推荐答案

启用ADL注释适用于

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

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

您是对的,ADL只需要一个不标准的名称,但这是重新编写代码的方式-

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

只是普通

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

不起作用,因为对于许多类型,ADL找不到 std :: swap ,并且没有其他可用的 swap 实现。

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

这篇关于如何“使用std :: swap”启用ADL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆