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

  • SEARCH
  • Home
  • 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 8151109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:16:14+00:00 2026-06-06T15:16:14+00:00

I understand that, without R-value references, perfect forwarding in C++ would be impossible. However,

  • 0

I understand that, without R-value references, perfect forwarding in C++ would be impossible.

However, I would like to know: is there anything else that necessitates them?


For example, this page points out this example in which R-value references are apparently necessary:

  X foo();
  X x;
  // perhaps use x in various ways
  x = foo();

The last line above:

  • Destructs the resource held by x,
  • Clones the resource from the temporary returned by foo,
  • Destructs the temporary and thereby releases its resource.

However, it seems to me that a simple change would have fixed the problem, if swap were implemented properly:

X foo();
X x;
// perhaps use x in various ways
{
    X y = foo();
    swap(x, y);
}

So it seems to me that r-value references not necessary for this optimization. (Is that correct?)

So, what are some problems that could not be solved with r-value references (except for perfect forwarding, about which I already know)?

  • 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-06T15:16:16+00:00Added an answer on June 6, 2026 at 3:16 pm

    So, what are some problems that could not be solved with r-value references (except for perfect forwarding, about which I already know)?

    Yes. In order for the swap trick to work (or at least, work optimally), the class must be designed to be in an empty state when constructed. Imagine a vector implementation that always reserved a few elements, rather than starting off totally empty. Swapping from such a default-constructed vector with an already existing vector would mean doing an extra allocation (in the default constructor of this vector implementation).

    With an actual move, the default-constructed object can have allocated data, but the moved-from object can be left in an unallocated state.

    Also, consider this code:

    std::unique_ptr<T> make_unique(...) {return std::unique_ptr<T>(new T(...));}
    
    std::unique_ptr<T> unique = make_unique();
    

    This is not possible without language-level move semantics. Why? Because of the second statement. By the standard, this is copy initialization. That, as the name suggests, requires the type to be copyable. And the whole point of unique_ptr is that it is, well, unique. IE: not copyable.

    If move semantics didn’t exist, you couldn’t return it. Oh yes, you can talk about copy elision and such, but remember: elision is an optimization. A conforming compiler must still detect that the copy constructor exists and is callable; the compiler simply has the option to not call it.

    So no, you can’t just emulate move semantics with swap. And quite frankly, even if you could, why would you ever want to?

    You have to write swap implementations yourself. So if you’ve got a class with 6 members, your swap function has to swap each member in turn. Recursively through all base classes and such too.

    C++11 (though not VC10 or VC2012) allows the compiler to write the move constructors for you.

    Yes, there are circumstances where you can get away without it. But they look incredibly hacky, are hard to see why you’re doing things that way, and most important of all, make things difficult for both the reader and the writer.

    When people want to do something a lot for performance sake that makes their code tedious to read, difficult to write, and error-prone to maintain (add another member to the class without adding it to the swap function, for example), then you’re looking at something that probably should become part of the language. Especially when it’s something that the compiler can handle quite easily.

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

Sidebar

Related Questions

I understand that Git can be used without a central repository. However, I am
How would I calculate the scrolling momentum for a scroll event? I understand that
I understand that if I pass a value-type ( int , struct , etc.)
I would like to clarify something. With plain Java reflection techniques (without using a
I understand that Web Site Projects compile source on-the-fly, and Web Application Projects pre-compile
I understand that CoCreateInstance finds the COM server for the given class id, creates
I understand that in MVVM: the View knows about the ViewModel the ViewModel knows
I understand that we need to create MXML file to define a view. Suppose
I understand that when adding a column to a table containing data in SQL
I understand that Perl's OO model is rather primitive; it is, in most respects,

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.