Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8899057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:45:50+00:00 2026-06-15T00:45:50+00:00

This program tries to move a string out of a function and use it

  • 0

This program tries to move a string out of a function and use it for the construction of another string:

#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);
}

The program compiles, but segfaults upon execution.


This was my rationale: Get_String will create a local string. A copy of that string will need to be made and returned before the string goes out of scope. That copy will be used to construct the string in main. However, If I moved the the string out of the function, no copy would need to be made.

In an attempt to understand move semantics, could someone explain why what I’m doing, probably makes no sense. Is it possible to move objects out of a function?


EDIT:
It compiles and runs correctly if I change the function signature from:

std::string && Get_String(void);

to

std::string Get_String(void);  

Is it still more efficient to move the string during the return in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:45:51+00:00Added an answer on June 15, 2026 at 12:45 am

    Given this example,

    X foo ()
    {
      X x;    
      return x;
    }
    

    the following behavior is guaranteed:

    • 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.

    Note also that returning an rvalue reference is an error if the returned object is a local nonstatic object:

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

    An rvalue reference is a reference, and returning it while referring to a local object means that you
    return a reference to an object that doesn’t exist any more. Whether std::move() is used doesn’t
    matter.

    std::move() doesn’t really move object; it only turns lvalues into rvalues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
This program builds a dictionary out of a list based on what two index
This program I use has it's own variables to set when you run it,
When compiling a program containing this particular function, /* * Function read_integer * *
Recently I tried compiling program something like this with GCC: int f(int i){ if(i<0){
I am using Eclipse to program in Java. I tried installing from this site
This program returns: package main import ( flag fmt ) func main() { num_agents
This program runs just fine, but for some reason when I ask for an
this program is for class to display the database in Listview.i want to add
This program stores pairs in a map, counting the number of times a word

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.