std :: move(const shared_ptr参考)是什么意思? [英] what does std::move(const shared_ptr reference) mean?

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

问题描述

以下是我正在尝试的玩具代码...我理解第一个和第二个.第一个将所有权赋予 _p .第二个将 p 复制到 _p .但我不明白第三个...

The following is toy code I am trying... I understand the first and second one. The first one give the ownership to _p. The second one copies p to _p. but I don't understand the third one...

const shared_ptr& std :: move 是什么意思?谢谢.

What does std::move of const shared_ptr & mean? Thank you.

class P { };

class A {
public:
    // first one
    A(std::shared_ptr<P> &p, int) : _p(std::move(p))
    {
        std::cout << "1st Ctor: "
                  << p.use_count() << ", " << _p.use_count() << std::endl;
    }

    // second one
    A(const std::shared_ptr<P> &p, std::string) : _p(p)
    {
        std::cout << "2nd Ctor: "
                  << p.use_count() << ", " << _p.use_count() << std::endl;
    }

    // third one
    A(const std::shared_ptr<P> &p) : _p(std::move(p))
    {
        std::cout << "3rd Ctor: "
                  << p.use_count() << ", " << _p.use_count() << std::endl;
    }

private:
    std::shared_ptr<P> _p;
};

int main()
{
    {
        std::shared_ptr<P> p = std::make_shared<P>();
        A a(p, 1);
        std::cout << "1. body: " << p.use_count() << std::endl;
    }
    std::cout << "-------------" << std::endl;
    {
        std::shared_ptr<P> p = std::make_shared<P>();
        A a(p, "2");
        std::cout << "2. body: " << p.use_count() << std::endl;
    }
    std::cout << "-------------" << std::endl;
    {
        std::shared_ptr<P> p = std::make_shared<P>();
        A a(p);
        std::cout << "3. body: " << p.use_count() << std::endl;
    }
 }

结果是:

$ ./a.out 
1st Ctor: 0, 1
1. body: 0
-------------
2nd Ctor: 2, 2
2. body: 2
-------------
3rd Ctor: 2, 2
3. body: 2

(已更新:添加注释以澄清是第一个,第二个,等等)

(updated: adding comment to clarify which one is first one, second one, etc.)

推荐答案

std :: move 是将参数转换为右值引用的函数.该函数调用是一个xvalue表达式.

std::move is a function which converts the argument to an rvalue reference. The function call is an xvalue expression.

当参数是对const的引用时,则转换结果是对const的右值.如果从rvalue初始化为const,将使用复制构造函数,因为move构造函数的 non-const 的rvalue引用参数无法绑定到const的rvalue引用参数.

When the argument is a reference to const, then the result of the conversion is an rvalue to const. If you initialise from rvalue to const, copy constructor will be used because the rvalue reference parameter to non-const of the move constructor cannot bind to rvalue reference argument to const.

我认为OP还存在一个隐含的问题,即 _p(std :: move(p)) _p(p)

const std :: shared_ptr< T>的情况下,

_p(std :: move(p)) _p(p)没有区别..

_p(std::move(p)) doesn't differ from _p(p) in case of const std::shared_ptr<T>.

理论上,如果 decltype(_p)是具有构造函数 T(const T&&)的类型,则会有区别,因为构造函数将由 _p(std :: move(p))调用,而不由 _p(p)调用.这样的构造函数将是非常常规的,但是在技术上是格式正确的. std :: shared_ptr 没有这样的构造函数.

In theory, if decltype(_p) was a type that had a constructor T(const T&&), then there would be a difference, since that constructor would be invoked by _p(std::move(p)) but not by _p(p). Such constructor would be quite unconventional, but technically well-formed. std::shared_ptr doesn't have such constructor.

这篇关于std :: move(const shared_ptr参考)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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