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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:18:11+00:00 2026-05-26T20:18:11+00:00

I’m new to move semantics in C++11 and I don’t know very well how

  • 0

I’m new to move semantics in C++11 and I don’t know very well how to handle unique_ptr parameters in constructors or functions. Consider this class referencing itself:

#include <memory>

class Base
{
  public:

    typedef unique_ptr<Base> UPtr;

    Base(){}
    Base(Base::UPtr n):next(std::move(n)){}

    virtual ~Base(){}

    void setNext(Base::UPtr n)
    {
      next = std::move(n);
    }

  protected :

    Base::UPtr next;

};

Is this how I should write functions taking unique_ptr arguments?

And do I need to use std::move in the calling code?

Base::UPtr b1;
Base::UPtr b2(new Base());

b1->setNext(b2); //should I write b1->setNext(std::move(b2)); instead?
  • 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-26T20:18:12+00:00Added an answer on May 26, 2026 at 8:18 pm

    Here are the possible ways to take a unique pointer as an argument, as well as their associated meaning.

    (A) By Value

    Base(std::unique_ptr<Base> n)
      : next(std::move(n)) {}
    

    In order for the user to call this, they must do one of the following:

    Base newBase(std::move(nextBase));
    Base fromTemp(std::unique_ptr<Base>(new Base(...));
    

    To take a unique pointer by value means that you are transferring ownership of the pointer to the function/object/etc in question. After newBase is constructed, nextBase is guaranteed to be empty. You don’t own the object, and you don’t even have a pointer to it anymore. It’s gone.

    This is ensured because we take the parameter by value. std::move doesn’t actually move anything; it’s just a fancy cast. std::move(nextBase) returns a Base&& that is an r-value reference to nextBase. That’s all it does.

    Because Base::Base(std::unique_ptr<Base> n) takes its argument by value rather than by r-value reference, C++ will automatically construct a temporary for us. It creates a std::unique_ptr<Base> from the Base&& that we gave the function via std::move(nextBase). It is the construction of this temporary that actually moves the value from nextBase into the function argument n.

    (B) By non-const l-value reference

    Base(std::unique_ptr<Base> &n)
      : next(std::move(n)) {}
    

    This has to be called on an actual l-value (a named variable). It cannot be called with a temporary like this:

    Base newBase(std::unique_ptr<Base>(new Base)); //Illegal in this case.
    

    The meaning of this is the same as the meaning of any other use of non-const references: the function may or may not claim ownership of the pointer. Given this code:

    Base newBase(nextBase);
    

    There is no guarantee that nextBase is empty. It may be empty; it may not. It really depends on what Base::Base(std::unique_ptr<Base> &n) wants to do. Because of that, it’s not very evident just from the function signature what’s going to happen; you have to read the implementation (or associated documentation).

    Because of that, I wouldn’t suggest this as an interface.

    (C) By const l-value reference

    Base(std::unique_ptr<Base> const &n);
    

    I don’t show an implementation, because you cannot move from a const&. By passing a const&, you are saying that the function can access the Base via the pointer, but it cannot store it anywhere. It cannot claim ownership of it.

    This can be useful. Not necessarily for your specific case, but it’s always good to be able to hand someone a pointer and know that they cannot (without breaking rules of C++, like no casting away const) claim ownership of it. They can’t store it. They can pass it to others, but those others have to abide by the same rules.

    (D) By r-value reference

    Base(std::unique_ptr<Base> &&n)
      : next(std::move(n)) {}
    

    This is more or less identical to the “by non-const l-value reference” case. The differences are two things.

    1. You can pass a temporary:

      Base newBase(std::unique_ptr<Base>(new Base)); //legal now..
      
    2. You must use std::move when passing non-temporary arguments.

    The latter is really the problem. If you see this line:

    Base newBase(std::move(nextBase));
    

    You have a reasonable expectation that, after this line completes, nextBase should be empty. It should have been moved from. After all, you have that std::move sitting there, telling you that movement has occurred.

    The problem is that it hasn’t. It is not guaranteed to have been moved from. It may have been moved from, but you will only know by looking at the source code. You cannot tell just from the function signature.

    Recommendations

    • (A) By Value: If you mean for a function to claim ownership of a unique_ptr, take it by value.
    • (C) By const l-value reference: If you mean for a function to simply use the unique_ptr for the duration of that function’s execution, take it by const&. Alternatively, pass a & or const& to the actual type pointed to, rather than using a unique_ptr.
    • (D) By r-value reference: If a function may or may not claim ownership (depending on internal code paths), then take it by &&. But I strongly advise against doing this whenever possible.

    How to manipulate unique_ptr

    You cannot copy a unique_ptr. You can only move it. The proper way to do this is with the std::move standard library function.

    If you take a unique_ptr by value, you can move from it freely. But movement doesn’t actually happen because of std::move. Take the following statement:

    std::unique_ptr<Base> newPtr(std::move(oldPtr));
    

    This is really two statements:

    std::unique_ptr<Base> &&temporary = std::move(oldPtr);
    std::unique_ptr<Base> newPtr(temporary);
    

    (note: The above code does not technically compile, since non-temporary r-value references are not actually r-values. It is here for demo purposes only).

    The temporary is just an r-value reference to oldPtr. It is in the constructor of newPtr where the movement happens. unique_ptr‘s move constructor (a constructor that takes a && to itself) is what does the actual movement.

    If you have a unique_ptr value and you want to store it somewhere, you must use std::move to do the storage.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.