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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:21:47+00:00 2026-05-17T00:21:47+00:00

If my class SomeType has a method that returns a element from the map

  • 0

If my class SomeType has a method that returns a element from the map (using the key) say

std::unique_ptr<OtherType> get_othertype(std::string name)
{
   return otMap.find(name);
}

that would enure the caller would recieve a pointer to the one in the map rather than a copy? Is it ok to do this, or would it try and call the copy constructor (and fail as it has been removed) because it is being returned?

Assuming I must use unique_ptr as my map items.

UPDATE::

After trying to implement the code, it seems that unique_ptr and std:map/:pair dont work together in gcc 4.4.4, pair just didnt like unique_ptr as a type parameter. (see Can't create map of MoveConstructibles).

I changed the ptr to std::shared_ptr and it all worked.

I suppose I can use the same code with the shared pointer?

  • 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-17T00:21:47+00:00Added an answer on May 17, 2026 at 12:21 am

    The model of unique_ptr is transfer of ownership. If you return a unique_ptr to an object from a function, then no other unique_ptr in the system can possibly refer to the same object.

    Is that what you want? I highly doubt it. Of course, you could simply return a raw pointer:

    OtherType* get_othertype(const std::string& name)
    {
        return otMap.find(name)->second.get();
    }
    

    Thus, the client has access to the object, but the map still owns it.

    The above solution is rather brittle in case there is no entry found under the name. A better solution would be to either throw an exception or return a null pointer in that case:

    #include <stdexcept>
    
    OtherType* get_othertype(const std::string& name)
    {
        auto it = otMap.find(name);
        if (it == otMap.end()) throw std::invalid_argument("entry not found");
        return it->second.get();
    }
    
    OtherType* get_othertype(const std::string& name)
    {
        auto it = otMap.find(name);
        return (it == otMap.end()) ? 0 : it->second.get();
    }
    

    And just for completeness, here is Anthony’s suggestion of returning a reference:

    OtherType& get_othertype(const std::string& name)
    {
        auto it = otMap.find(name);
        if (it == otMap.end()) throw std::invalid_argument("entry not found");
        return *(it->second);
    }
    

    And here is how you return a reference to the unique_ptr inside the map, but let’s make that a reference to const, so the client does not accidentally modify the original:

    unique_ptr<OtherType> const& get_othertype(const std::string& name)
    {
        auto it = otMap.find(name);
        if (it == otMap.end()) throw std::invalid_argument("entry not found");
        return it->second;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C# question. Say I have a customers class that has a bunch of props
I'm trying to write a class that has a generic member variable but is
I have a working codebase that has a class called Tabs. All methods and
I have a method that returns byte[] but for now would like to avoid
Suppose some type Foo has an overloaded operator-> that returns a Bar* : struct
I have a class Derived<T> that is derived from Base<T> . Which of the
I have a class that has a propery List<String> or List<SomeObject> . I get
What is the difference between this two codes? class SomeClass { SomeType val =
class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,
Is there a efficient way to provide an Enumarable<SomeType> or a collection that implements

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.