Boost logo

Boost Users :

From: Alo Sarv (alo.sarv_at_[hidden])
Date: 2005-12-22 19:43:04


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net