I want to use map in C++, but I don’t use exceptions. After using map<int, int> my_map in a function, how can I check if it managed to allocate memory internally (I understand that with exceptions any new inside that can’t allocate memory will throw an exception)?
I want to use map in C++ , but I don’t use exceptions. After
Share
Whether or not you use exceptions, the standard containers will (if they are using their default allocators) throw
std::bad_allocif a memory allocation fails. If you don’t catch this, then your program will terminate – so there is no way to check for success without catching the exception in this case.If you really want to eradicate exceptions (which in my opinion is a bad idea, even if you don’t want to use the Standard Library), then you will have ditch the standard containers, rewriting whatever containers you want to use a non-standard allocation model, and check for and propagate any failures. The standard containers all assume that allocation will either succeed or throw, so they cannot be used with an allocator that doesn’t give that guarantee.