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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:47:32+00:00 2026-06-17T22:47:32+00:00

I have code as following std::vector<std::unique_ptr<int>> v; std::unique_ptr<int> a(new int(0)); std::unique_ptr<int>& b = a;

  • 0

I have code as following

std::vector<std::unique_ptr<int>> v;
std::unique_ptr<int> a(new int(0));
std::unique_ptr<int>& b = a;
v.insert(v.begin(), std::move(b)); //ok

However if I add const in the third statement

const std::unique_ptr<int>& b = a;
v.insert(v.begin(), std::move(b)); //Compiler error, cannot access ptr private member

Why does the compiler show it cannot access private member of the unique pointer other than cannot convert const to non-const? Thanks.

  • 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-17T22:47:34+00:00Added an answer on June 17, 2026 at 10:47 pm

    You cannot move a const object. Moving means altering the state of the object you moved from (of whatever type that object is), and declaring it as const makes a promise that you won’t alter that state. Thus, moving a const object is a contradiction in terms.

    Although the expectation of being able to move a const object may indeed have some kind of justification (for instance, you may want the object to be const as long as you use it, and then get rid of it when you’re done and transfer its guts rather than copying it – which is btw impossible with a unique_ptr), actually allowing this would have a number of very impractical semantic consequences (for instance, it would allow swapping two const objects).

    Thus, even though the language does technically allow moving from a const object (move constructors accepting a const RRef are legal), no type of the Standard Library actually gives you that possibility, including unique_ptr, and you should not write any UDT that does.

    Conceptually, it is really important to think of moving as a state-altering operation, which is not compatible with the const modifier.

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

Sidebar

Related Questions

I have the following code: int MimeDocument::GetAttachmentId( std::string const& content_id ) { using namespace
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
I have the following lines of code: if(std::binary_search(face_verts.begin(), face_verts.end(), left_right_vert[0]) && std::binary_search(face_verts.begin(), face_verts.end(), left_right_vert[1]))
Lets say we have the following code: std::vector<int> f() { std::vector<int> y; ... return
I have the following code: void Foo() { static std::vector<int>(3); // Vector object is
I have the following code: const uint8_t HEADER_SIZE = 0x08; std::vector<uint8_t> a, b; uint8_t
I have the following code #include <vector> #include <iostream> class A{ private: std::vector<int> x;
Suppose i have the following code: std::vector< std::pair <int, char> > myVec; or std::list<
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc

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.