paul23
paul23

Reputation: 9435

overload std::find for custom classes

Well is it possible/"good" to overload std::find for custom classes in objects? And then without overloading the equality operators. Say a container stores (smart) pointers to it's actual data, I really am not interested in the data layout or storage mode, I just want to find a certain data member....

Is it legal, "moral", to o this? If say I have a std::vector<std::shared_ptr<myClass> > - or "even" a user defined container? Or should I always rely on std::find_if for cases like these?

Upvotes: 0

Views: 462

Answers (1)

juanchopanza
juanchopanza

Reputation: 227420

The idiomatic solution would be to use std::find_if and give it a suitable predicate.

This keeps the code you have to write and maintain to a minimum, and decouples you from the specific container type.

Upvotes: 5

Related Questions