|
Boost Users : |
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2008-05-07 05:19:02
Bill Buklis <boostuser <at> pbjzone.com> writes:
>
> I would like to able to provide two versions of a function where one is
> enabled for only POD types and other another if the object provides a usable
> member function (mainly used for compatible non POD types). Anything else
> should fail to compile. The first part is easy. I can do this with:
>
> template< class T >
> typename boost::enable_if< boost::is_pod<T>, void >::type
> DoSomething( T& object )
> {
> ...
> }
>
> But, I'm not sure how to do the second part. For example:
>
> template< class T >
> typename boost::enable_if< has "DoSomething" member, void >::type
> DoSomething( T& object )
> {
> Object.DoSomething();
> }
Take a look at is_call_possible (http://preview.tinyurl.com/4x86m8)
and can_be_called (http://preview.tinyurl.com/52rkj7).
> Perhaps it would be easier to check for a defined typedef inside the object?
> But, I'm not sure on that either.
>
> Additionally, if possible: If both conditions can be satisfied then the
> second version (member function) should be preferred.
>
> Is this possible?
Yes, it's possible.
template <class T>
struct handle_pods {
static do(T&);
};
template <class T>
struct handle_class_with_method {
static do(T& t) {
t.DoSomething();
}
};
template <class T>
void DoSomething(T& t) {
typedef typename mpl::if_<
has_do_something_method<T>,
handle_class_with_method<T>,
typename mpl::if<
is_pod<T>,
handle_pod<T>,
int
>::type
>::type impl;
impl::do(t);
}
HTH,
Roman Perepelitsa.
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