Currently I have a own version of a linked list in a std::unordered_map<key,List<shared_ptr<t>>>
(where t is some type). I insert stuff like so:
void addTo(const Key &key, t* ptr)
{
if(ptr==nullptr||ptr==NULL){
throw "this is not a real exception that I am using"
}
try
{
this->getList(key).addNew(std::shared_ptr<t>(ptr));
}
catch(std::out_of_range ex)
{
this->addList(key,(std::shared_ptr<t>(ptr));
}
}
However, I recently noticed that when the getList throws a out_of_range exception (which it always does if the map doesnt have a key like that in it) then the ptr gets deleted. Now it would make sense if the shared_ptr was created before getList is called, but why would that be the case?
I have verfied this by simply seperating this->getList and addNew like so:
const List<shared_ptr<t>> l& = this->getList(key);
l.addNew(etc etc);
When writing it like this, nothing happens to the pointer.
This leads me to conclude that the shared_ptr gets created first, which would mean that c++ processes instancing first? To me that doesnt make a while lot of sense.
What are the exact rules for the order in which c++ does these kind of statements and why is it not instancing the shared_ptr AFTER ->getList (what is the reason for doing it differently)?