Topic: operator ->* in smart pointers


Author: Sean Hunt <rideau3@gmail.com>
Date: Tue, 13 Oct 2009 11:58:16 CST
Raw View
On Oct 11, 5:04 pm, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> I think it's not obvious that this actually forwards A::value as an lvalue
> behind the scene. I would consequently not provide this operator for safety
> reasons, and require users to write the slightly longer

The requirement for a definition of A::value exists with any other
forwarding function; I don't think that's a really good reason to
prevent a user-defined ->*.

Sean Hunt


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Sean Hunt <rideau3@gmail.com>
Date: Wed, 7 Oct 2009 14:07:30 CST
Raw View
Now that the technology (perfect forwarding) exists to make it
possible, is there any particular reason the standard library does not
provide an operator ->* in smart pointers? It could return either a
reference or a functor, as appropriate.

While I'm not sure about the implications of this, perhaps smart
pointers should also provide an operator() that performs a forwarding
call, so that bind(smart_ptr, &type::func, foo, bar) works as it would
with a raw pointer?

Sean Hunt

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: cpp4ever <n2xssvv.g02gfr12930@ntlworld.com>
Date: Thu, 8 Oct 2009 14:20:27 CST
Raw View
On 07/10/09 21:07, Sean Hunt wrote:

    Now that the technology (perfect forwarding) exists to make it
    possible, is there any particular reason the standard library does not
    provide an operator ->* in smart pointers? It could return either a
    reference or a functor, as appropriate.

    While I'm not sure about the implications of this, perhaps smart
    pointers should also provide an operator() that performs a forwarding
    call, so that bind(smart_ptr,&type::func, foo, bar) works as it would
    with a raw pointer?

    Sean Hunt


Out of curiosity I implemented this type of smart pointer and it's not
pretty and only covered member function pointers for functions of up
to 3 parameters. If you want to gain experience in functors, templates
and even partial template specialisation then it's a worthwhile
exercise, but for everyday use its easier to do the following with
standard auto pointers

       (*ap).*mp
       ((*ap).*mp)(/*function parameters if necessary*/)

       ap .... auto pointer
       mp .... member pointer

Well, that's my opinion based on experience. I hope it helps.

Regards

JB

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Sean Hunt <rideau3@gmail.com>
Date: Thu, 8 Oct 2009 17:54:08 CST
Raw View
On Oct 8, 2:20 pm, cpp4ever <n2xssvv.g02gfr12...@ntlworld.com> wrote:
> On 07/10/09 21:07, Sean Hunt wrote:
>
>     Now that the technology (perfect forwarding) exists to make it
>     possible, is there any particular reason the standard library does not
>     provide an operator ->* in smart pointers? It could return either a
>     reference or a functor, as appropriate.
>
>     While I'm not sure about the implications of this, perhaps smart
>     pointers should also provide an operator() that performs a forwarding
>     call, so that bind(smart_ptr,&type::func, foo, bar) works as it would
>     with a raw pointer?
>
>     Sean Hunt
>
> Out of curiosity I implemented this type of smart pointer and it's not
> pretty and only covered member function pointers for functions of up
> to 3 parameters. If you want to gain experience in functors, templates
> and even partial template specialisation then it's a worthwhile
> exercise, but for everyday use its easier to do the following with
> standard auto pointers

C++0x makes it possible to do it perfectly for any number of
parameters, which is why I raised the question - it couldn't be done
before, but now it can be.

Sean Hunt


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sun, 11 Oct 2009 17:04:19 CST
Raw View
Sean Hunt wrote:

> Now that the technology (perfect forwarding) exists to make it
> possible, is there any particular reason the standard library does not
> provide an operator ->* in smart pointers? It could return either a
> reference or a functor, as appropriate.
>

The behavior is slightly different for direct and indirect calls, which
could lead to nasty surprises:

struct A {
   static int const value = 1;
};

void f(int) { }

template<typename T>
void ffwd(T &&t) {
   f(static_cast<T&&>(t));
}

I think that users of ffwd(A::value) would expect behavior to be exactly the
same as doing f(A::value). But since A::value is an lvalue, we will bind
A::value to an lvalue reference before calling f, which will make us need a
definition of A::value.

This of course applies to all forwarding calls, but especially to

(ptr->*memptr)(A::value)

I think it's not obvious that this actually forwards A::value as an lvalue
behind the scene. I would consequently not provide this operator for safety
reasons, and require users to write the slightly longer

((*ptr).*memptr)(A::value)

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]