@@ -488,9 +488,13 @@ template <class ElemTy> struct ConcurrentReadableArray {
488
488
~Snapshot () {
489
489
Array->decrementReaders ();
490
490
}
491
-
492
- const ElemTy *begin () { return Start; }
493
- const ElemTy *end () { return Start + Count; }
491
+
492
+ // These are marked as ref-qualified (the &) to make sure they can't be
493
+ // called on temporaries, since the temporary would be destroyed before the
494
+ // return value can be used, making it invalid.
495
+ const ElemTy *begin () & { return Start; }
496
+ const ElemTy *end () & { return Start + Count; }
497
+
494
498
size_t count () { return Count; }
495
499
};
496
500
@@ -974,7 +978,11 @@ struct ConcurrentReadableHashMap {
974
978
975
979
// / Search for an element matching the given key. Returns a pointer to the
976
980
// / found element, or nullptr if no matching element exists.
977
- template <class KeyTy > const ElemTy *find (const KeyTy &key) {
981
+ //
982
+ // This is marked as ref-qualified (the &) to make sure it can't be called
983
+ // on temporaries, since the temporary would be destroyed before the return
984
+ // value can be used, making it invalid.
985
+ template <class KeyTy > const ElemTy *find (const KeyTy &key) & {
978
986
if (!Indices.Value || !ElementCount || !Elements)
979
987
return nullptr ;
980
988
return ConcurrentReadableHashMap::find (key, Indices, ElementCount,
@@ -1181,7 +1189,8 @@ struct StableAddressConcurrentReadableHashMap
1181
1189
}
1182
1190
1183
1191
template <class KeyTy > ElemTy *find (const KeyTy &key) {
1184
- auto result = this ->snapshot ().find (key);
1192
+ auto snapshot = this ->snapshot ();
1193
+ auto result = snapshot.find (key);
1185
1194
if (!result)
1186
1195
return nullptr ;
1187
1196
return result->Ptr ;
0 commit comments