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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:59:01+00:00 2026-06-04T00:59:01+00:00

I’m having the following (simplified) class: class Operator { private: std::map<std::string, std::unique_ptr<Operand>> op; public:

  • 0

I’m having the following (simplified) class:

class Operator {
private:
    std::map<std::string, std::unique_ptr<Operand>> op;

public:
    template <class T>
    void insertOperand(std::string const &s, T o = T()) {
        op.insert(std::pair<std::string, std::unique_ptr<StreamOperand>>(
            s, std::move(std::unique_ptr<T>(new T(o)))
        );
    }

    void setOperandsValue(std::string const &o, int v) {
        op.find(o)->second->setValue(v);
    }
};

Inserting a new Operand works without any problems. However, when the function returns the destructor gets called and thus the map does not contain any object when calling setOperandsValue. I’ve observed this using DDD: at the end of insertOperand Operator::~Operator() is called.

After looking at Using std::unique_ptr with STL, I introduced (better: used) std::move but either it’s not placed correctly or I am missing something (highly probable due to lack of knowledge). I’m not using map::emplace because it’s not available.

Edit: The destructor call is valid, since it’s destroying new T(o). Anyway, the map remains empty when entering setOperandsValue.

Edit #2: On entering setOperandsValue and performing op.find(o) the result is op.end, i.e. entry not found although I’ve added it before.

  • 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-04T00:59:01+00:00Added an answer on June 4, 2026 at 12:59 am

    I don’t think your pointer is getting destroyed. What you are seeing here:

    template <class T>
    void insertOperand(std::string &s, T o = T()) {
        op.insert(std::pair<std::string, std::unique_ptr<StreamOperand>>(
            s, std::move(std::unique_ptr<T>(new T(o)))
        );
    }
    

    is the destruction of o, after it has been used to construct the heap allocated T used in the unique_ptr.

    The map being empty is not a symptom of the pointer being destroyed. If this were the case (the pointer being destroyed), you would have an entry in the map for a given key, with an invalid unique_ptr.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.