理解 std::swap().tr1::_Remove_reference 的目的是什么? [英] Understanding std::swap(). What is the purpose of tr1::_Remove_reference?

查看:22
本文介绍了理解 std::swap().tr1::_Remove_reference 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VS10 的 STL 实现中有以下代码用于 std::swap() 的变体:

In the STL implementation of VS10 there is the following code for a variant of std::swap():

    // TEMPLATE FUNCTION _Move
template<class _Ty> inline
    typename tr1::_Remove_reference<_Ty>::_Type&&
        _Move(_Ty&& _Arg)
    {   // forward _Arg as movable
    return ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg);
    }

    // TEMPLATE FUNCTION swap (from <algorithm>)
template<class _Ty> inline
    void swap(_Ty& _Left, _Ty& _Right)
    {   // exchange values stored at _Left and _Right
    _Ty _Tmp = _Move(_Left);
    _Left = _Move(_Right);
    _Right = _Move(_Tmp);
    }

在 _Move() 中对 (typename tr1::_Remove_reference<_Ty>::_Type&&) 的转换是我问题的来源.

The cast to (typename tr1::_Remove_reference<_Ty>::_Type&&) in _Move() is the source of my question.

在这种情况下,_Remove_reference 定义如下:

In this context _Remove_reference is defined as follows:

        // TEMPLATE _Remove_reference
template<class _Ty>
    struct _Remove_reference
    {   // remove reference
    typedef _Ty _Type;
    };

_Remove_reference 有什么作用,为什么?此外,什么是 &&在这里做,它是如何命名的?

What does _Remove_reference do, and why? Furthermore, what does && do here, and how is it termed?

推荐答案

_Remove_reference 是一个模板,可以从其类型中删除引用.我很确定该模板专门用于 T&T&& 具有相同的 typedef,对吗?

_Remove_reference is a template that, well, removes the reference from its type. I'm pretty sure that the template is specialized for T& and T&& with the same typedef, right?

不知道我应该在这里详细介绍多少.您熟悉模板元编程吗?

Not sure how much I should go into details here. Are you familiar with template metaprogramming?

_Move 似乎与 std::move 所做的完全一样:它将其参数转换为右值.练习的重点是 std::swap 可以做移动而不是复制.你熟悉移动语义吗?

_Move seems to do exactly what std::move does: it casts its argument to an rvalue. The point of the exercise is that std::swap can do moves instead of copies. Are you familiar with move semantics?

什么是 &&在这里做,它是如何命名的?

what does && do here, and how is it termed?

您要查找的术语是右值引用".观看 此视频 并带着问题回来.

The term you are looking for is "rvalue reference". Watch this video and come back with questions.

这篇关于理解 std::swap().tr1::_Remove_reference 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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