0

I would like to use the std::vector without exceptions. In my section of code, I am using all new calls with the (std::nothrow) to prevent it to throw.

But in case of a vector, I do not know how to use it so it will never throw an error but rather fail the insert() or push_back() call.

Wrapping the push_back() call with a try/catch is not a solution, this shall work without using exception handling at all.

// create a vector
std::vector<int> *V;

// here I can prevent exceptions.
V = new (std::nothrow) std::vector<int>();
if(!V)
    {handle_error();return;}

// Can I prevent this to throw anything and rather get an error result?
V->push_back(42);
Blindleistung
  • 672
  • 8
  • 21
  • https://stackoverflow.com/a/7277756/14086496 – Anthony Dec 17 '20 at 11:27
  • @JulianH Yes, I think that gives the answer. Either use an own allocator which I can guarantee to succeed (or verify before calling push_back()), or use of std containers is limited. Sadly, that drops the convenience of the vector, as the code becomes more cumbersome than just reaallocating arrays myself... – Blindleistung Dec 17 '20 at 14:17
  • The STL is poorly designed in many ways, however there are some improved STL-like libraries out there, maybe you will find what you search there. E.g. EASTL or boost. – JulianW Dec 17 '20 at 17:00

0 Answers0