A functor that calls a function after dereferencing?

Is there a small functor in the C ++ standard or in Boost that wraps around another functor but shares a pointer before it calls this functor?

I am thinking of something like this:

template<class F> struct DerefCmp { template<class T> bool operator()(T* v) const { return F(*v); } }; 

I would use it in a pointer container, for example, where I want to compare by value:

 std::set<int*, DerefCmp< std::equal<int> > > s; 
+4
source share
2 answers

I am not aware of any functional object in the standard C ++ library or in Boost that does this (which does not mean that it is not there, I am not familiar with everything in Boost libraries: -P).

However, writing your own is easy enough. Consider the following:

 template <typename Predicate> class indirect_binary_predicate { public: indirect_binary_predicate(const Predicate& pred = Predicate()) : pred_(pred) { } template <typename Argument0, typename Argument1> bool operator()(Argument0 arg0, Argument1 arg1) const { return pred_(*arg0, *arg1); } private: Predicate pred_; }; 

Usage example:

 std::set<int*, indirect_binary_predicate<std::equal_to<int> > > s; 

Please note that it is not recommended to have a container with unprocessed pointers if pointers are designed for dynamically distributed objects, and the container has ownership of objects with a pointer to objects; this is not an exception - it is safe. However, this predicate adapter should work just as well for smart pointers, iterators, or any other type that supports dereferencing.

+3
source

You can see containers of acceleration pointers. They can be used for this purpose with standard functors: Boost.PointerContainer

+2
source

All Articles