Tessil / sparse-map

C++ implementation of a memory efficient hash map and hash set
MIT License
334 stars 36 forks source link

fix const value return by emplace iterator #24

Open firedtoad opened 1 year ago

firedtoad commented 1 year ago

with const this code will not compile tsl::sparse_map<uint32_t,uint32_t> spp; const auto it=spp.emplace(1,1); it.first->second=1;

Tessil commented 1 year ago

Thank you for the contribution, unfortunately casting a std::pair<K, V>& to a std::pair<const K, V>& is an undefined behaviour. See https://stackoverflow.com/questions/8305793/is-it-possible-to-cast-a-pairkey-value-to-a-pairconst-key-value for more information.

firedtoad commented 1 year ago

It is not undefined behaviour, because they have the same memory layout. I use this modified version in my project. And it passed the clang && gcc UBsan!

Thibaut Goetghebuer-Planchon @.***> 于2023年10月10日周二 03:25写道:

Casting a std::pair<K, V>& to a std::pair<const K, V>& is an undefined behaviour. See https://stackoverflow.com/questions/8305793/is-it-possible-to-cast-a-pairkey-value-to-a-pairconst-key-value for more information.

— Reply to this email directly, view it on GitHub https://github.com/Tessil/sparse-map/pull/24#issuecomment-1753571067, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK6QJWABV22VUVRPYKJM2LX6RFSHAVCNFSM6AAAAAAYKU2DOGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJTGU3TCMBWG4 . You are receiving this because you authored the thread.Message ID: @.***>

firedtoad commented 1 year ago

The new standard shows that it is layout compatible. https://godbolt.org/z/dKn99YTeM