Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-02-23 11:46:58


----- Original Message -----
From: "rogeeff" <rogeeff_at_[hidden]>

> > > Anyone knows a fix?
> >
> > It depends whether 'A' is supposed to represent a user-defined type
> or a
> > template supplied by the library. If the latter, you can intrude on
> foo and
> > detect that T is an A<T> using the sizeof() trick, then dispatch as
> > appropriate. Otherwise, I'm afraid there's no way.
> >
> > -Dave
>
> It's latter. Could you clarify the trick, please?

// No testing; off the top of my head:

template <class T>
yes_type is_A_helper(A<T> const&);
no_type is_A_helper(...);

template <class T>
struct is_A_instantiation
{
    static T& f();

    BOOST_STATIC_CONSTANT(bool, value = sizeof(is_A_helper(F())) ==
sizeof(yes_type));
};

template <bool b> struct is_A_tag{};

template <class T>
void foo_dispatch(T const&, is_A_tag<false>) { ... }

template <class T>
void foo_dispatch(A<T> const&, is_A_tag<true>) { ... }

template <class T>
void foo(T const& x)
{
   foo_dispatch(x, is_A_tag<is_A_instantiation<T>::value>());
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk