The purpose behind my question is to use std::map
(ie insertion, deletion, access) with guarantee that no exception will throw from std::map
implementation.
Deletion is not a problem, neither access if one use things carefully (no at
without check for example).
But insertion is another problem. In this code:
#include <map>
struct foo{};
int main()
{
std::map<int, foo> my_map;
my_map.insert(std::pair<int, foo>(42, foo{}));
}
my_map
do some allocation and throw if there is no more memory.
Now, what I want is a way to do this:
#include <map>
struct foo{};
int main()
{
std::map<int, foo> my_map;
auto my_new_pair{ new std::pair<int, foo>(42, foo{}) };
if (my_new_pair) // ok, we could build the node
{
my_map.insert(my_new_pair); // no allocation, my_map only takes the "ownership" of my_new_pair
}
}
but there is no such overload of insert
.
Do you guys have a solution?