Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ unordered_map exception safety

I was wandering the C++ specifications (cplusplus.com) and found that there's nothing told about exception safety for std::unordered_map

so basically if I write

map["foo"]=5;

and an exception is thrown because I'm out of memory or bad_alloc, what can I assume about my map?

  • It is left in a valid state? (basic guarantee)
  • It is left unchanged? (strong guarantee)
  • It is left in unvalid state ? (no guarantee)
like image 904
CoffeDeveloper Avatar asked Mar 05 '26 10:03

CoffeDeveloper


1 Answers

23.2.4.1 Exception safety guarantees [associative.reqmts.except]

2 For associative containers, if an exception is thrown by any operation from within an insert or emplace function inserting a single element, the insertion has no eftect.

This paragraph applies to the indexing operator. So:

  • It is left unchanged. (strong guarantee)
like image 195
Howard Hinnant Avatar answered Mar 07 '26 23:03

Howard Hinnant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!