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 5987677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:50:59+00:00 2026-05-22T22:50:59+00:00

Lets say we have the following code: std::vector<int> f() { std::vector<int> y; … return

  • 0

Lets say we have the following code:

std::vector<int> f()
{
  std::vector<int> y;
  ...
  return y;
} 

std::vector<int> x = ...
x = f();

It seems the compiler has two approaches here:

(a) NRVO: Destruct x, then construct f() in place of x.
(b) Move: Construct f() in temp space, move f() into x, destruct f().

Is the compiler free to use either approach, according to the standard?

  • 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-05-22T22:51:00+00:00Added an answer on May 22, 2026 at 10:51 pm

    The compiler may NRVO into a temp space, or move construct into a temp space. From there it will move assign x.

    Update:

    Any time you’re tempted to optimize with rvalue references, and you’re not positive of the results, create yourself an example class that keeps track of its state:

    • constructed
    • default constructed
    • moved from
    • destructed

    And run that class through your test. For example:

    #include <iostream>
    #include <cassert>
    
    class A
    {
        int state_;
    public:
        enum {destructed = -2, moved_from, default_constructed};
    
        A() : state_(default_constructed) {}
        A(const A& a) : state_(a.state_) {}
        A& operator=(const A& a) {state_ = a.state_; return *this;}
        A(A&& a) : state_(a.state_) {a.state_ = moved_from;}
        A& operator=(A&& a)
            {state_ = a.state_; a.state_ = moved_from; return *this;}
        ~A() {state_ = destructed;}
    
        explicit A(int s) : state_(s) {assert(state_ > default_constructed);}
    
        friend
        std::ostream&
        operator<<(std::ostream& os, const A& a)
        {
            switch (a.state_)
            {
            case A::destructed:
                os << "A is destructed\n";
                break;
            case A::moved_from:
                os << "A is moved from\n";
                break;
            case A::default_constructed:
                os << "A is default constructed\n";
                break;
            default:
                os << "A = " << a.state_ << '\n';
                break;
            }
            return os;
        }
    
        friend bool operator==(const A& x, const A& y)
            {return x.state_ == y.state_;}
        friend bool operator<(const A& x, const A& y)
            {return x.state_ < y.state_;}
    };
    
    A&& f()
    {
        A y;
        return std::move(y);
    }
    
    int main()
    {
        A a = f();
        std::cout << a;
    }
    

    If it helps, put print statements in the special members that you’re interested in (e.g. copy constructor, move constructor, etc.).

    Btw, if this segfaults on you, don’t worry. It segfaults for me too. Thus this particular design (returning an rvalue reference to a local variable) is not a good design. On your system, instead of segfaulting, it may print out “A is destructed”. This would be another sign that you don’t want to do this.

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

Sidebar

Related Questions

Lets say I have the following code: delegate int MyDel (int n); // my
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have the following: public class MyObject { [Bindable] public var foo:int
Lets say I have the following code public static string GetXMLValue() { XDocument settingsFile
Lets say I have the following code : PreparedStatement ps = null; ResultSet rs
I have following scenario: Lets say we have two different webparts operating on the
Let's say I have the following code: IBOutlet UITextField* nameTextField; IBOutlet UILabel* greetingLabel; I'd
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
Let's say that I have the following code in application_helper.rb : def do_something if
Let's say you have the following chunk of code: <div id=container> <someelement>This is any

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.