Re: Bound member functions
Sean Hunt ha scritto:
Overloading of the ->* operator is a good reason. In a smart pointer
class (assuming T is the type pointed to, ptr is pointer being
wrapped.):
template <typename U>
inline auto operator ->* (U T::* p) // Sorry if I got the syntax
wrong. You get the idea, right?
-> ptr->*p
I guess you mean
-> decltype(ptr->*p)
{
if (!ptr)
throw myCustomException;
return ptr->*p;
}
Without a bound member function as a type, this won't work. Any other
solution simply won't cover the full range of possibilities that can
be done with this, such as ensuring all operators work for any type.
Yes, that's precisely one use case and a very good one.
I'd like to correct myself twice here. First, it should probably
return a reference, which I overlooked, [...]
No. According to my proposal, decltype(ptr->*p) is a type than can be
safely copied and should actually returned by value.
return a reference, which I overlooked, but more importantly, operator
->* can be implemented using type traits, but it's difficult:
template <typename U, bool b = std::is_function<U>>
U& operator ->* (U T::* p)
{
return ptr->*p;
}
template <typename U>
auto operator ->*<U, true> (U T::* p)
-> std::bind(std::mem_fn(p), ptr)
Again, you missed the decltype here.
{
return std::bind(std::mem_fn(p), ptr)
}
the call to mem_fn is actually unnecessary. you can simply write
std::bind(p, ptr).
Anyway, your example is good for member functions that has exactly no
parameters. What if they have some? Let's rewrite the example, the
difference is minimal:
template <typename U, typename... Args>
inline auto operator ->* (U (T::*p)(Args...))
-> decltype(ptr->*p)
{
return ptr->*p;
}
(notice that you no longer need to rely on std::is_function nor on
std::bind)
I challenge you to rewrite the same code with std::bind. You need to
convert the template argument pack Args to a sequence of placeholders
_1, _2, etc. There may be MPL techniques able to do that, but I bet that
it's going to be painful.
HTH,
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]