Boost logo

Boost :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2002-04-23 16:04:22


----- Original Message -----
From: "David Abrahams" <david.abrahams_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, April 23, 2002 8:26 AM
Subject: Re: [boost] Re: Re: Quick Question...

> As far as I'm concerned, Paul is the inventor. I may have made some
> tweaks to get it into boost, but that's all.
>
> -Dave

No, I just used 'template deduction failure' to eliminate an overload from the
candidate set when I was implementing 'is_enum'--based on the fact that an
enumeration cannot the the 'class' types in a pointer to member signature and
eliminating everything else that also can't be there. I think it might have
been Peter Dimov that first use the same type of mechanism to determine if a
nested type exists or not. But I may be wrong, I can't remember. From there,
several other usages came out of it such as 'is_class'.

But this is the first time I've seen something like this (maybe I'm the inventor
of this instead):

// find if a class has a data member (of a given type)

template<class T> struct has_int_data_x {
    private:
        template<class U> struct helper {
            template<int U::*> struct inner { };
        };
        template<class U> static small_t check(typename helper<U>::template
inner<&U::x>*);
        template<class U> static large_t check(...);
    public:
        static const bool value = sizeof(check<T>(0)) == sizeof(small_t);
};

template<class T> const bool has_int_data_x<T>::value;

// find if a class has a member function (of a given type)

template<class T> struct has_void_f_void { // nice name huh?
    private:
        template<class U> struct helper {
            template<void (U::*)(void)> struct inner { };
        };
        template<class U> static small_t check(typename helper<U>::template
inner<&U::f>*);
        template<class U> static large_t check(...);
    public:
        static const bool value = sizeof(check<T>(0)) == sizeof(small_t);
};

The crappy part about these is that you can't parametize the name that you are
looking for. You need a separate implementation for each name.

Paul Mensonides


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