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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:28:23+00:00 2026-06-16T02:28:23+00:00

Consider this program: #include <map> #include <string> #define log magic_log_function // Please don’t mind

  • 0

Consider this program:

#include <map>
#include <string>
#define log magic_log_function // Please don't mind this.

//
// ADVENTURES OF PROGO THE C++ PROGRAM
//

class element;
typedef std::map<int, element> map_t;

class element {
public:
    element(const std::string&);
    element(const element&);
    ~element();
    std::string name;
};
element::element(const std::string& arg)
    : name(arg)
{
    log("element ", arg, " constucted, ", this);
}
element::element(const element& other)
    : name(other.name)
{
    name += "-copy";
    log("element ", name, " copied, ", this);
}
element::~element()
{
    log("element ", name, " destructed, ", this);
}
int main(int argc, char **argv)
{
    map_t map1; element b1("b1");
    log(" > Done construction.");
    log(" > Making map 1.");
    map1.insert(std::pair<int, element>(1, b1));
    log(" > Done making map 1.");
    log(" > Before returning from main()");
}

It creates some objects on stack and inserts them into an std::map container, creating two extra temporary copies in the process:

element b1 constucted, 0x7fff228c6c60
 > Done construction.
 > Making map 1.
element b1-copy copied, 0x7fff228c6ca8
element b1-copy-copy copied, 0x7fff228c6c98
element b1-copy-copy-copy copied, 0x232d0c8
element b1-copy-copy destructed, 0x7fff228c6c98
element b1-copy destructed, 0x7fff228c6ca8
 > Done making map 1.
 > Before returning from main()
element b1 destructed, 0x7fff228c6c60
element b1-copy-copy-copy destructed, 0x232d0c8

We can get rid of one extra copy constructor call by changing the std::pair signature to std::pair<int, element&>, however, the second temporary is still created and immediately destroyed:

element b1 constucted, 0x7fff0fe75390
 > Done construction.
 > Making map 1.
element b1-copy copied, 0x7fff0fe753c8
element b1-copy-copy copied, 0x1bc4098
element b1-copy destructed, 0x7fff0fe753c8
 > Done making map 1.
 > Before returning from main()
element b1 destructed, 0x7fff0fe75390
element b1-copy-copy destructed, 0x1bc4098

Is there a way to make std::map just take an object on stack by reference and make a single internal copy of it?

  • 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-16T02:28:25+00:00Added an answer on June 16, 2026 at 2:28 am

    Standard practice (with older C++ versions) where I’ve been is to use a Map of shared pointers.

    Still creates a copy of the shared pointers, but that’s usually much less onerous than copying large objects.

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

Sidebar

Related Questions

Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void
Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
Consider this small program: #include <stdio.h> #include <stdlib.h> // Change 60000 to 70000 and
Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider the following C++ program: #include <cstdlib> // for exit(3) #include <string> #include <iostream>
Consider the following program: #define _POSIX_C_SOURCE 200809L #include <time.h> #include <pthread.h> #include <signal.h> void
Consider this little program: #include <stdio.h> int main() { char c = 0xFF; printf(%d\n,
Consider this pointless program: /* main.c */ #include <stdlib.h> #include <unistd.h> int main(int argc,
Consider the following program: #include <string> struct S { S (){} private: void *ptr

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.