Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-01-30 12:07:18


On Thu, 30 Jan 2003 16:34:44 +0200, "Rani Sharoni"
<rani_sharoni_at_[hidden]> wrote:

>
>"Gennaro Prota" <gennaro_prota_at_[hidden]> wrote in message
>> 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.

Yes! Not in mind, just in my editor though :-)

>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)
> };
>};

> <test cases snipped>

My version was only slightly different:

template <typename B, typename D>
struct checker {
  typedef char yes;
  typedef char(&no)[2];

  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 C
    {
        operator B const volatile *() const;
        operator D const volatile *();
    };

public:
    static const bool result =
        sizeof(checker<B,D>::check( C() , 0 )) ==
        sizeof(typename checker<B, D>::yes);
};

Of course moving yes and no outside checker<> is cleaner, but I tried
to avoid changes not forced by compiler bugs. Incidentally, I noticed
that if you add a default argument

  template<typename T>
  static yes check(D const volatile *, T = 0);
  static no check(B const volatile *, int = 0);

and write:

  sizeof(checker<B,D>::check( (C()) /*, 0*/ ))

then ambiguity problems arise, even with Comeau 4.3.0.1. Defect in the
standard?

Genny.


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