Operator ->* in smart pointers

  • Thread starter Dario Saccavino
  • Start date
D

Dario Saccavino

It seems to me that operator->* is overloadable in C++ (just like
operator->), and it should be useful to implement in smart pointer
classes.

However, I haven't found its implementation either in the standard
weak_ptr class, or in any boost smart pointer. Was it intentionally
left out of those headers?
Actually there's a workaround: just write
(ptr.operator->())->*memptr
instead of
ptr->*memptr
But it looks very ugly. How am I supposed to use member pointers with
smart pointers?

Thank you for your attention

Dario
 
A

Alf P. Steinbach

* Dario Saccavino:
It seems to me that operator->* is overloadable in C++ (just like
operator->), and it should be useful to implement in smart pointer
classes.

However, I haven't found its implementation either in the standard
weak_ptr class, or in any boost smart pointer. Was it intentionally
left out of those headers?
Actually there's a workaround: just write
(ptr.operator->())->*memptr
instead of
ptr->*memptr
But it looks very ugly. How am I supposed to use member pointers with
smart pointers?

In general it's not a good idea to use member pointers.

They're tricky and yield unmaintainable code, they can circumvent access
control, and mostly they indicate design errors. Regard member pointers
as being in the same class of constructs as goto. Sometimes a necessary
evil, but not something you'd employ without due consideration.

If you absolutely must, try something like

#include <iostream>
#include <boost/shared_ptr.hpp>

struct Foo
{
int value;
Foo( int x = 42 ): value( x ) {};
};

template< typename Result, typename SmartPtr, typename Class >
Result& operator->*( SmartPtr& p, Result Class::* pMember )
{
return (&*p)->*pMember; // Not guaranteed there is get().
}

int main()
{
boost::shared_ptr<Foo> p( new Foo );
int Foo::* pFooInt = &Foo::value;

p->*pFooInt = 43;
std::cout << p->value << std::endl;
}

Note that to be general, the operator's result type must be adjusted to
the union of the cv-qualification of the two arguments.

And that's non-trivial.


Cheers, & hth.,

- Alf
 
J

James Kanze

* Dario Saccavino:
In general it's not a good idea to use member pointers.

Except when the alternatives are worse:).

Seriously, I've used them a couple of times in the past, and
almost every time, I've ended up going back, and reconverting
the code to use delegates. Which requires a bit more
boilerplate code, but is more flexible and more easily
understood by other programmers.

As for why std::auto_ptr (and the Boost smart pointers) don't
support them, the main reason is probably because they're an
open set. You'd need something like:
template< typename U >
??? operator->*( U op ) ;
And I'm not sure what you'd have to put in place of the ??? if U
was a pointer to member function---the result of p->*pmf is a
bound member function, and C++ doesn't have a type for it. (And
of course, the very few times you might want to use pointers to
members, it will be with a pointer to member function.)

Anyway, given that it's neither useful nor implementable, I can
sort of understand it not being present, even if it is an
incompatibility with raw pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top