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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:52:25+00:00 2026-05-27T11:52:25+00:00

#include <vector> #include <iostream> #include <memory> int main() { // create the vector std::vector<

  • 0
#include <vector>
#include <iostream>
#include <memory>

int main()
{
    // create the vector
    std::vector< std::unique_ptr< int > > v;
    for ( int i = 0; i < 5; ++ i )
    {
        std::unique_ptr< int > newItem( new int(i) );
        v.push_back( std::move( newItem ) );
    }

    std::cout<<"vector's size before the move = " << v.size() << std::endl;

    // move one item
    auto it = move( v[2] );
    std::cout<<"moved element = " << *it << std::endl;
    std::cout<<"vector's size after the move = " << v.size() << std::endl;
if ( nullptr == v[2].get())
    std::cout<<"it is nullptr" << std::endl;
}

The above example is moving one element out of the vector, but the vector’s size remained the same.

What contains the moved element after the move? Is it an undefined behaviour to access that element? Is it nullptr (the example prints it is null)?

  • 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-27T11:52:26+00:00Added an answer on May 27, 2026 at 11:52 am

    It’s nothing to do with the vector. std::uniqe_ptr defines the state of the object after a move, it’s set to null (20.7.1/4).

    In general, an object that has been moved from has a valid but unspecified state. So unless the type documents cases in which it cannot be accessed, you can access it. Particular classes arguably should give more detail of that state whenever possible, but what vector relies on is that:

    1. the object can be moved from again (otherwise the vector wouldn’t be able to reallocate)
    2. the object can still be destroyed when the vector is destroyed.

    So, for a type other than unique_ptr in the vector, you should probably think to yourself that the resulting value of the source of the move can be any of:

    1. an “empty” or “zero” value of the type,
    2. the original value,
    3. the original value of the target of the move,
    4. some other value of the type that you didn’t think of.

    If you still think it’s worth accessing a value that might be any of those, go ahead 😉

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

Sidebar

Related Questions

#include<iostream> #include<vector> std::vector<std::string> vector1; int main() { vector1.push_back(adadad); std::vector<std::string> vector2; vector2.push_back(adadd); if (vector1==vector2) {
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
#include <iostream> #include <vector> #include <list> using namespace std; int main() { vector<list<string> >
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
#include <iostream> #include <algorithm> #include <vector> #include <boost/array.hpp> #include <boost/bind.hpp> int main() { boost::array<int,
#include <iostream> #include <vector> int main() { class Int { public: Int(int _i) :
#include iostream #include vector using namespace std; const vector<int>& Getv() { vector<int> w(10); w[0]=10;
#include <iostream> #include <vector> using namespace std; int in; bool isPrime(int n) { for

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.