unchain_ptr functionality?
Hi all When writing generic code, it's sometimes needed to call a member function of a passed object, however it's not known at the implementation time whether the function is passed a pointer or reference to the object. For example: template<typename T> void process(T t) { t.foo(); } This breaks as soon as the function is passed a pointer (or smart pointer) type, which means one needs to either provide overloads for all those cases (inconvenient), or have means of unchaining the pointer (it could also be pointer-to-pointer for example). Basically I'm looking for syntax like this: template<typename T> void process(T t) { unchain_ptr(t).foo(); } struct X { void foo() {} }; X *x = new X; process(x); process(&x); process(*x); process(boost::shared_ptr<X>(x)); // etc Is there such an utility in Boost libraries? Currently I wrote the implementation myself, and achieved the expected syntax, but perhaps there's an existing implementation already that I could use instead? I saw some libraries (multi_index) using something like this internally, but there doesn't seem to be any "end-user" API for this as far as I can see... Alo Sarv
Alo Sarv wrote: [...]
Basically I'm looking for syntax like this:
template<typename T> void process(T t) { unchain_ptr(t).foo(); } struct X { void foo() {} }; X *x = new X; process(x); process(&x); process(*x); process(boost::shared_ptr<X>(x)); // etc
Is there such an utility in Boost libraries?
mem_fn comes to mind: template<typename T> void process(T t) { mem_fn( &X::foo )( t ); } It's not exactly the same, though.
participants (2)
-
Alo Sarv
-
Peter Dimov