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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:06:01+00:00 2026-05-18T08:06:01+00:00

unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can

  • 0

unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr<T> from a function and assign the returned value to a variable.

#include <iostream>
#include <memory>

using namespace std;

unique_ptr<int> foo()
{
  unique_ptr<int> p( new int(10) );

  return p;                   // 1
  //return move( p );         // 2
}

int main()
{
  unique_ptr<int> p = foo();

  cout << *p << endl;
  return 0;
}

The code above compiles and works as intended. So how is it that line 1 doesn’t invoke the copy constructor and result in compiler errors? If I had to use line 2 instead it’d make sense (using line 2 works as well, but we’re not required to do so).

I know C++0x allows this exception to unique_ptr since the return value is a temporary object that will be destroyed as soon as the function exits, thus guaranteeing the uniqueness of the returned pointer. I’m curious about how this is implemented, is it special cased in the compiler or is there some other clause in the language specification that this exploits?

  • 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-18T08:06:02+00:00Added an answer on May 18, 2026 at 8:06 am

    is there some other clause in the language specification that this exploits?

    Yes, see 12.8 §34 and §35:

    When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object […]
    This elision of copy/move operations, called copy elision, is permitted […]
    in a return statement in a function with a class return type, when the expression is the name of
    a non-volatile automatic object
    with the same cv-unqualified type as the function return type […]

    When the criteria for elision of a copy operation are met and the object to be copied is designated by an lvalue,
    overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.


    Just wanted to add one more point that returning by value should be the default choice here because a named value in the return statement in the worst case, i.e. without elisions in C++11, C++14 and C++17 is treated as an rvalue. So for example the following function compiles with the -fno-elide-constructors flag

    std::unique_ptr<int> get_unique() {
      auto ptr = std::unique_ptr<int>{new int{2}}; // <- 1
      return ptr; // <- 2, moved into the to be returned unique_ptr
    }
    
    ...
    
    auto int_uptr = get_unique(); // <- 3
    

    With the flag set on compilation there are two moves (1 and 2) happening in this function and then one move later on (3).

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

Sidebar

Related Questions

I am confused with unique_ptr and rvalue move philosophy. Let's say we have two
How unique is the php session id? I got the impression from various things
How to get unique Google Gadget ID from a gadget added to a iGoogle
In the actual C++ standard, creating collections satisfying following rules is hard if not
One of the C++0x improvements that will allow to write more efficient C++ code
I have a class containing a std::unique_ptr<> and I want to put instances of
Will auto_ptr be deprecated in incoming C++ standard? Should unique_ptr be used for ownership
I'm currently trying to use Howard Hinnant's unique_ptr implementation , and am running into
Is a GUID unique 100% of the time? Will it stay unique over multiple
After using array_unique , an array without the duplicate values is removed. However, it

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.