Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Edit: am no longer seeking resolution . will resolve my particular problem via some means yet to be determined perhaps reference counting perhaps class type per template scalar value . still some investigating to do .

map.insert and map.emplace as shown below result in the destructor being called of the contained class . this does not occur for vector.push_back . i would like to inquire as to why the destructor is being called and how to avoid this occurrence if possible as it interferes w/ my code . Thank You Kindly

#include <iostream>
#include <vector>
#include <map>
using namespace std;

#define LINE_FUNCSIG cout << __LINE__ << ' ' << __FUNCSIG__<< endl;
#define COMMA ,
#define SHOW_CODE(_code_) cout << #_code_ << ' '<< endl; _code_

struct cfoo
{
     cfoo() { cout << "constructor "; LINE_FUNCSIG }
     cfoo(const cfoo&) { cout <<"copy constructor "; LINE_FUNCSIG }
     cfoo(cfoo&&) { cout << "move constructor "; LINE_FUNCSIG }
     ~cfoo() { cout<<"destructor "; LINE_FUNCSIG }
};

int main()
{
     SHOW_CODE(cfoo _cfoo;)
     vector<cfoo> _vector;
     SHOW_CODE(_vector.push_back(_cfoo);)
     map<int, cfoo> _map;
     SHOW_CODE(_map.insert({ 159 COMMA _cfoo });)
     SHOW_CODE(_map.emplace(357 COMMA cfoo());)
     cout << "FINI" << endl;
     return 0;
}

output of above :
Terminal
cfoo _cfoo;
constructor 12 __cdecl cfoo::cfoo(void)
_vector.push_back(_cfoo);
copy constructor 13 __cdecl cfoo::cfoo(const struct cfoo &)
_map.insert({ 159 COMMA _cfoo });
copy constructor 13 __cdecl cfoo::cfoo(const struct cfoo &)
move constructor 14 __cdecl cfoo::cfoo(struct cfoo &&)
destructor 15 __cdecl cfoo::~cfoo(void)
_map.emplace(357 COMMA cfoo());
constructor 12 __cdecl cfoo::cfoo(void)
move constructor 14 __cdecl cfoo::cfoo(struct cfoo &&)
destructor 15 __cdecl cfoo::~cfoo(void)
FINI
destructor 15 __cdecl cfoo::~cfoo(void)
destructor 15 __cdecl cfoo::~cfoo(void)
destructor 15 __cdecl cfoo::~cfoo(void)
destructor 15 __cdecl cfoo::~cfoo(void)


What I have tried:

did some searching on web re/ map
Posted
Updated 5-Feb-23 6:10am
v3

1 solution

If you step through the code in the debugger you will be able to see exactly what it is doing. You can also check the documentation at std::map - cppreference.com[^].
 
Share this answer
 
Comments
BernardIE5317 5-Feb-23 11:22am    
i did both prior to posting . i know what it is doing . what i do not know is why it is doing it or how to avoid the destructor or if it possible to do so . i can think of no reason for the destructor to be called . in any event i can solve my particular problem via reference counting so i consider this problem closed . i do not know the CP protocol for such situations . do i delete the post . do i post my own solution to "Add your solution here" . ¯\_(ツ)_/¯
Richard MacCutchan 5-Feb-23 11:55am    
You can just edit the question and add [Solved] to the title.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900