Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a shared_ptr in an unordered_set with only a const shared_ptr?

I have an unordered_set<shared_ptr<T>> us and I would like to know whether a needle k is in us, but k has type shared_ptr<T const> so unordered_set<shared_ptr<T>>::find complains that it cannot convert.

Is there a way around this? Maybe by directly supplying the hash?

I did try const_cast (and felt dirty) but that didn't cut it.

like image 255
bitmask Avatar asked Jun 08 '18 10:06

bitmask


1 Answers

Using std::const_pointer_cast is a possible solution here.

us.find(std::const_pointer_cast<T>(k));

Since you're not modifying k, it's okay to cast away the const.

like image 67
DeiDei Avatar answered Oct 20 '22 14:10

DeiDei