类方法实现体内的“using std::swap”是什么意思?
我试图根据这个问题的全面解释来学习并采用复制交换习惯用法:复制交换习惯用法。
但我发现了一些我从未见过的代码: using std::swap; // 在此示例中允许 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
}
};
- 在函数实现体内,
using std::swap;
意味着什么? - ADL 是什么意思?
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
}
};
- what does
using std::swap;
mean inside the body of a function implementation ? - what does ADL mean ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种机制通常用在模板化代码中,即
template。 Foo 类。
现在的问题是使用哪个交换。
std::swap
可以工作,但可能并不理想。对于Value
类型,很有可能有更好的swap
重载,但那会在哪个命名空间中呢?它几乎肯定不在std::
中(因为这是非法的),但很可能在Value
的命名空间中。有可能,但还远未确定。在这种情况下,
swap(myValue, anotherValue)
将为您提供尽可能的“最佳”交换。参数相关查找将在Value
来源的命名空间中查找任何交换。否则,using
指令将启动,std::swap
将被实例化并使用。在您的代码中,
mSize
可能是一个整数类型,而mArray
是一个指针。两者都没有关联的命名空间,并且std::swap
无论如何都具有 99.9% 的确定性对它们来说是最佳的。因此,using std::swap;
声明在这里似乎毫无用处。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 ofswap
for typeValue
, but in which namespace would that be? It's almost certainly not instd::
(since that's illegal), but quite likely in the namespace ofValue
. 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 whereValue
came from. Otherwise theusing
directive kicks in, andstd::swap<Value>
will be instantiated and used.In your code,
mSize
is likely an integral type, andmArray
a pointer. Neither has an associated namespace, andstd::swap
is with 99.9% certainty optimal for them anyway. Therefore, theusing std::swap;
declaration seems useless here.using
关键字具有作用域效果。这意味着在
using
关键字范围内,std::swap
可以被称为swap
。The
using
keyword has scoped effect.This means that
std::swap
can be referred to asswap
during the scope of theusing
keyword.通过添加
using std::swap
,它可以在该函数中提供更广泛的灵活性。交换没有特定于类型的版本,因为它是模板,因此为任何内置类型调用 std 版本都不会产生问题。假设 Foo 类有一个成员 h 保存类型 ExampleType,并且我们为 ExampleType 定义了类型特定的 swap 版本:
上面的示例将仅调用 std 版本,而不是我们的 ExampleType 版本。因此,通过包含
using std::swap
,程序将调用我们定义的版本,而不是std::swap
:即使我们用内置函数替换了正在交换的参数,代码仍然会执行而不会出现错误,因为编译器会推断出 std 版本将是最佳选择。此概念也称为参数相关查找或 ADL。
By adding
using std::swap
it allows for a wider range of flexibility within that function. There is no type specific version for swap because it is a template, so calling the std versions for any build-in type yields no problems.Assuming class Foo has a member h that holds type ExampleType and we define a type specific version of swap for ExampleType:
The example above would solely call the std version and not our ExampleType version. So by including
using std::swap
the program will call our defined version and notstd::swap
:Even if we replaced arguments being swapped with build-ins, the code would still execute without errors because the compiler would deduce that the std version would be the best option. This concept is also known as Argument Dependent Lookup or ADL.