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

这个程序试图将一个字符串移出一个函数,并用它来构造另一个字符串:
#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()并不真正移动对象;它只会将价值变成价值.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...