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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:18:21+00:00 2026-05-31T13:18:21+00:00

In the following code snippet, are there any caveats that need to be considered?

  • 0

In the following code snippet, are there any caveats that need to be considered?

void do_stuff()
{
    std::unique_ptr<my_type> my(new my());

    my_type& mine = *my;

    //use mine 
}

It gives me a weird feeling to ‘bring out’ the value of my by dereferencing the implicit pointer, as it feels like it would create a temporary (but I’m quite sure that’s not the case)

  • 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-31T13:18:22+00:00Added an answer on May 31, 2026 at 1:18 pm

    The code is fine with the usual caveats when holding a reference (or pointer) to an object: you have to make sure that the object outlives the uses of the pointer or reference.

    In this particular case, make sure that the object inside the unique_ptr does not get deleted by a call to reset() or delete my.release(); which I think are the only cases where the lifetime of that reference might outlive the lifetime of the object itself.

    Side note: I am assuming that you have a good reason to use dynamic allocation for that object. If you are not sure, consider not using pointers/new in the first place.

    I am also assuming that the reference is not there for performance, if it is, you might want to consider as access through the reference or the smart pointer will be translated to exactly the same binary.

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

Sidebar

Related Questions

Is there any performance penalty for the following code snippet? for (int i=0; i<someValue;
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Problem There is a program file that contains the following code snippet at some
I have the following code snippet: void foo(double a) {} namespace bar_space { struct
Is there any differences between following code snippets? I'm using VS 2010, .NET 4,
In the following code snippet, does the check for dic.isEmpty() result in any performance
Is there any ready for use code snippet in VS 2010 Express edition (for
Instruments is informing me that I am leaking memory from the following code snippet
The following code snippet illustrates a memory leak when opening XPS files. If you
The following code snippet on SQL server 2005 fails on the ampersand '&': select

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.