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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:37:14+00:00 2026-05-27T15:37:14+00:00

This code works as expected (online here ). At the end v is empty

  • 0

This code works as expected (online here).
At the end v is empty and w is not empty as it has pilfered the contents of v.

    vector<int> v;
    v.push_back(1);
    cout << "v.size(): " << v.size() << endl;
    auto vp = move(v);
    vector<int> w(vp);
    cout << "w.size(): " << w.size() << endl;
    cout << "v.size(): " << v.size() << endl;

But if I replace auto vp=move(v) with

    vector<int> && vp = move (v);

Then it doesn’t move. Instead it copies and both vectors are non-empty at the end. As shown here.

Clarification: More specifically, what is the auto-derived type of vp? If it’s not vector<int> &&, then what else could it be? Why do the two examples give different results despite being so similar?

Extra: I also tried this, and it still copied instead of moving

    std :: remove_reference< vector<int> > :: type && vp = move(v);
  • 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-27T15:37:15+00:00Added an answer on May 27, 2026 at 3:37 pm

    Edit for OP’s clarification: the auto-derived type of move(v) is vector<int>. See C++11 "auto" semantics.

    The 1st example does this:

    move 'v' into 'vp'
    copy 'vp' into 'w'
    

    and the 2nd example does this:

    set 'vp' as rvalue-reference of 'v'
    copy 'vp' (which is 'v') into 'w'
    

    What std:move does is simply casting a type to an rvalue (See What is std::move(), and when should it be used?). Therefore, in

    vector<int>&& vp = move(v);
    

    it’s just setting the rvalue-reference vp to v and do nothing else. Also, an rvalue-reference is an lvalue (it has a name), so

    vector<int> w(vp);
    

    will call the copy constructor to copy vp (which is v) into w.

    It will call the move constructor if you make vp an rvalue (Example):

    vector<int> w(move(vp))
    

    You may want to read this: C++ Rvalue References Explained.

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

Sidebar

Related Questions

I have this code which compiles and works as expected: class Right {}; class
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code works in a windows forms application (it shows the preview) but not
This code works correctly to make a web service call: int numberOfGuests = Convert.ToInt32(search.Guest);
Right, so this is strange. This code works as expected: HTML: <section> Section #1
This code works as expected (does nothing, even doesn't produce warning/errors): l = lambda
After reloading the page this code works as expected - javascript works and opens
This code works fine: $result = $client->__call(optionalInfo, array( new SoapParam(..., client), new SoapParam(..., add_code))
This code works, but i dont understand why. With DeferredLoadingEnabld = false, I would
This code works: monkey.h @interface monkey : NSObject { NSNumber *monkeyRanch; } @property (nonatomic,

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.