加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

是否可以std ::将对象移出功能? (C 11)

发布时间:2020-12-16 06:44:05 所属栏目:百科 来源:网络整理
导读:这个程序试图将一个字符串移出一个函数,并用它来构造另一个字符串: #include iostream#include string#include utilitystd::string Get_String(void);int main(){ std::string str{Get_String()}; std::cout str std::endl; return 0;}std::string Get_Stri
这个程序试图将一个字符串移出一个函数,并用它来构造另一个字符串:
#include <iostream>
#include <string>
#include <utility>

std::string && Get_String(void);

int main(){

    std::string str{Get_String()};
    std::cout << str << std::endl;

    return 0;
}

std::string && Get_String(void){

    std::string str{"hello world"};

    return std::move(str);
}

该程序编译,但在执行时发生segfaults.

这是我的理由:Get_String将创建一个本地字符串.该字符串的副本将需要在字符串超出范围之前进行并返回.该副本将用于构造主要的字符串.但是,如果我将字符串移出该函数,则不需要复制.

为了理解移动语义,有人可以解释为什么我在做什么,可能没有任何意义.是否可以将对象移出功能?

编辑:
如果我改变功能签名,它会编译并正确运行:

std::string && Get_String(void);

std::string Get_String(void);

在这种情况下,在返回时移动字符串还是更有效吗?

解决方法

给出这个例子,
X foo ()
{
  X x;    
  return x;
}

以下行为得到保证:

? If X has an accessible copy or move constructor,the compiler may
choose to elide the copy. This is the so-called (named) return value
optimization ((N)RVO),which was specified even before C++11 and is
supported by most compilers.
? Otherwise,if X has a move constructor,x is moved.
? Otherwise,if X has a copy constructor,x is copied.
? Otherwise,a compile-time error is emitted.

还要注意,如果返回的对象是本地非静态对象,则返回一个rvalue引用是一个错误:

X&& foo ()
{
  X x;
  return x; // ERROR: returns reference to nonexistent object
}

引用引用是一个引用,并且在引用本地对象时返回它,这意味着你
返回对不再存在的对象的引用.是否使用std :: move()
物.

std :: move()并不真正移动对象;它只会将价值变成价值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读