Opened 18 years ago
Closed 18 years ago
#288 closed Feature Requests (None)
adapters for smart_ptr?
| Reported by: | nobody | Owned by: | Peter Dimov |
|---|---|---|---|
| Milestone: | Component: | smart_ptr | |
| Version: | None | Severity: | |
| Keywords: | Cc: |
Description
Hello,
May be I just can't find this, but...
I would like to use the STL algorithms on STL
containers of smart_ptrs. So I need an adapter that
adapts a member function of a class that is pointed
to by a smart_ptr. The standard boost::mem_fun does
not do this.
So, I wrote this class:
template <template<class> class PTR, typename S,
typename T>
class mem_fun_ptr_t : public std::unary_function<T*,
S>
{
public:
explicit mem_fun_ptr_t(S (T::*p)())
:
ptr(p)
{}
S operator()(PTR<T> p) const
{
return ((*p).*ptr)();
}
private:
S (T::*ptr)();
};
template <template<class> class PTR, typename S,
typename T>
inline mem_fun_ptr_t<PTR,S,T> mem_fun_ptr(S
(T::*f)())
{
return mem_fun_ptr_t<PTR,S,T>(f);
}
//============================
//
// usage (e.g.):
// =============
// class MyClass {
// public:
// void method() { /* Do something */ }
// };
// //...
// std::vector<shared_ptr<MyClass> > myVector;
// //... (fill myVector)
//
// std::for_each(myVector.begin(),myVector.end(),
// simon::mem_fun_ptr<shared_ptr>(&MyClass::method));
//
May be I am re-inventing the wheel?
(If so please tell me :^)
If not, could something like this be added to boost?
Thanks,
Simon Robbins.
robbins@physik.uni-wuppertal.de
Note:
See TracTickets
for help on using tickets.
