Boost logo

Boost :

From: Rani Sharoni (rani_sharoni_at_[hidden])
Date: 2003-01-30 10:17:44


"Rani Sharoni" <rani_sharoni_at_[hidden]> wrote in message
news:b1bd2p$i97$1_at_main.gmane.org...
>
> "Gennaro Prota" <gennaro_prota_at_[hidden]> wrote in message
> news:s18i3vorccgs6tmju273jl75b2bc896gl5_at_4ax.com...
> > On Thu, 30 Jan 2003 12:38:36 -0000, "John Maddock"
> > <jm_at_[hidden]> wrote:
> >
> > >Except it *doesn't work* !
> > >
> >
> > John, unfortunately I have to turn off my computer now. I had just
> > hacked up a version that seems to work with gcc, but I don't want to
> > post it before a better testing. I'll let you know in the morning.
>
> Maybe you had the following variation on Terje's code in mind.
>
> The following worked with GCC3.1/2, VC6/7/7.1 and Comeau 4.3.0.1:
>
>
> typedef char (&yes)[1];
> typedef char (&no) [2];
>
> template <typename B, typename D>
> struct helper
> {
> template <typename T>
> static yes check(D const volatile *, T);
> static no check(B const volatile *, int);
> };
>
> template<typename B, typename D>
> struct is_base_and_derived
> {
> struct Host
> {
> operator B const volatile *() const;
> operator D const volatile *();
> };
>
> enum { result =
> sizeof(helper<B,D>::check(Host(), 0)) == sizeof(yes)
> };
> };

Here is a weird (but working !) version:

template <typename B, typename D>
struct helper
{
    template <typename T>
    static yes check(int B::*, T);
    static no check(int D::*, int);
};

// TODO: add cv-qualifiers to B and D

template<typename B, typename D>
struct is_base_and_derived
{
    struct Host
    {
        operator int D::*() const;
        operator int B::*();
    };

    enum { result =
        sizeof(helper<B,D>::check(Host(), 0)) == sizeof(yes)
    };
};

BTW: the relevant section in the C++ standard is 13.3.3.2/4/3 (especially
the foot note).

Cheers,
Rani


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