Boost logo

Boost Users :

From: Bill Buklis (boostuser_at_[hidden])
Date: 2007-06-06 16:16:51


Good point. I was definitely making it more complicated than it needed to
be. OK, given that what would be the best way to declare/define the
operator* function. The following works for pointer types:

typename boost::remove_pointer<T>::type& operator*() const
{
    return(*data);
}

How can this be done for non-pointer types? I'm thinking that in addition to
a "get" type function which simply returns data, I could use operator* to
return the underlying data for all types. Pointers dereference, while
non-pointers return itself.

Of course, the real question is would this even be a good thing to do?

Without overloading the return type, sort of:

typename boost::remove_pointer<T>::type& operator*() const
{
    return( DEREFENCE_ONLY_IF_A_POINTER(data) );
}

-- Bill --

> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of me22
> Sent: Wednesday, June 06, 2007 1:24 PM
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] Operator overloading and enable_if
>
> On 06/06/07, Bill Buklis <boostuser_at_[hidden]> wrote:
> > For a template class I'd like to add * and -> operators for types that
> > support it (mainly pointers). However, most instantiations of this class
> > won't be pointers and therefore these operators won't be valid. I
> understand
> > that enable_if can't directly support member functions. But, how can I
> do
> > the equivalent of this?
> >
> > [code snipped]
> >
> SFINAE -- you don't even need boost:
>
> template <typename T>
> struct holder {
> T value;
> T operator->() const { return value; }
> };
>
> struct foo { int bar; };
>
> int main() {
> holder<foo*> a; // compiles
> a->bar = 0; // compiles
> holder<int> b; // compiles
> // b->bar = 0; // wouldn't compile
> }
>
> ~ Scott McMurray
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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