|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2003-01-29 01:12:27
Terje Slettebø <tslettebo_at_[hidden]> writes:
>>From: "Rani Sharoni" <rani_sharoni_at_[hidden]>
>
>> template <typename B, typename D>
>> struct is_base_and_derived
>> {
>> private:
>> typedef char (&yes)[1];
>> typedef char (&no) [2];
>>
>> template<typename T>
>> static yes check(D const volatile &, T);
>> static no check(B const volatile &, int);
>>
>> struct C
>> {
>> operator B const volatile &() const;
>> operator D const volatile &();
>> };
>>
>> static C getC();
>> public:
>> static const bool result =
>> sizeof(check(getC(), 0)) == sizeof(yes);
>> };
>>
>> Additional specializations needed (e.g. void and reference types)
>
> Very nice, Rani. Very clever. :)
I'm only not commenting yet because I don't get it yet.
> It's interesting that, according to your clc++m posting, the inspiration of
> this came from Andrei Alexandrescu's article on Mojo, who also came up with
> Loki's SuperSubclass trait. Dave Abrahams is also credited in the thread,
> for contributing to the above technique.
I don't think I deserve credit for anything above. What I did for
Mojo has to do with using derivation to force some precedence in the
overload resolution (according to rules I probably never really
understood, but had an intuitive feeling for).
> As you show in the other posting with example use, the above version works
> as overload resolution is done before access check, so it works also for
> private base classes. Besides, in this case, it never gets to do an access
> check, as the actual call is never done.
Oh, well maybe it has a remote connection to my Mojo contribution, but
that would depend on my having understood the mechanism at work (which
I clearly did not ;->).
> As I understand, the reason it works for multiple occurrences of the same
> base class is that it's not asking for D's B subobject, but instead for a
> conversion to _a_ B (any B).
>
> Also, "static no check(B const volatile &,int)" works as an ambiguity
> buster, in the case where B is not a base class of D.
Nice explanation. I'll have to look again tomorrow when I have a few
more brain cells at my disposal...
-- David Abrahams dave_at_[hidden] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk