类方法实现体内的“using std::swap”是什么意思?

发布于 2024-10-14 14:07:00 字数 627 浏览 9 评论 0原文

我试图根据这个问题的全面解释来学习并采用复制交换习惯用法:复制交换习惯用法

但我发现了一些我从未见过的代码: 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
    }
};
  1. 在函数实现体内,using std::swap; 意味着什么?
  2. 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
    }
};
  1. what does using std::swap; mean inside the body of a function implementation ?
  2. what does ADL mean ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2024-10-21 14:07:00

这种机制通常用在模板化代码中,即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 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.

枕花眠 2024-10-21 14:07:00

using 关键字具有作用域效果。

这意味着在 using 关键字范围内,std::swap 可以被称为 swap

The using keyword has scoped effect.

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

浅浅淡淡 2024-10-21 14:07:00

通过添加using std::swap,它可以在该函数中提供更广泛的灵活性。交换没有特定于类型的版本,因为它是模板,因此为任何内置类型调用 std 版本都不会产生问题。

假设 Foo 类有一个成员 h 保存类型 ExampleType,并且我们为 ExampleType 定义了类型特定的 swap 版本:

void swap(Foo& lhs, Foo& rhs)
{
    //ERROR: This would only the call std version 
    std::swap(lhs.h, rhs.h);
}

上面的示例将仅调用 std 版本,而不是我们的 ExampleType 版本。因此,通过包含using std::swap,程序将调用我们定义的版本,而不是std::swap

void swap(Foo& lhs, Foo& rhs)
{
    using std::swap;
    std::swap(lhs.h, rhs.h);   //Using class defined version of 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:

void swap(Foo& lhs, Foo& rhs)
{
    //ERROR: This would only the call std version 
    std::swap(lhs.h, rhs.h);
}

The example above would solely call the std version and not our ExampleType version. So by includingusing std::swap the program will call our defined version and not std::swap:

void swap(Foo& lhs, Foo& rhs)
{
    using std::swap;
    std::swap(lhs.h, rhs.h);   //Using class defined version of 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文