Boost logo

Boost :

From: Rani Sharoni (rani_sharoni_at_[hidden])
Date: 2003-12-10 04:55:57


Jonathan Turkanis wrote:
> This is a small point, since the code in question is hidden with '#if
> 0'; however, there seem to be several typos in the definition of
> is_abstract.
I posted possible implementation for this important trait (and bother Jason
Shirk to implement it in VC7.1) while ago and wondered where can I see the
one that you are referring to?

Anyway, I simplify my original proposal some time ago and you might want to
consider the new implementation (tested using Comeau 4.3.0.1 and VC7.1+ with
boost 1.30.2):

#include "boost/type_traits/is_class.hpp"
#include "boost/mpl/if.hpp"

template<typename T>
struct is_abstract_class
{
private:
    typedef typename boost::mpl::if_c<boost::is_class<T>::value, T,
int>::type TT;

    typedef char (&yes)[1];
    typedef char (&no) [2];

    //
    // Deduction fails if U is void, function type, reference type
(14.8.2/2)
    // or an abstract class type and according to WP issue #337
    // http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#337
    // supported by EDG3.0+ and VC7.1+
    //
    template<typename U>
    static no check(U (*)[1]);

    template<typename U>
    static yes check(...);

public:
    static const bool value = sizeof(check<TT>(0)) == sizeof(yes);
};

// tests
struct A { virtual ~A() = 0; };

typedef int Test[ is_abstract_class<A>::value];

struct B {};

typedef int Test[!is_abstract_class<B>::value];
typedef int Test[!is_abstract_class<int>::value];
typedef int Test[!is_abstract_class<int(char,double)>::value];
typedef int Test[!is_abstract_class<int&>::value];
typedef int Test[!is_abstract_class<void>::value];

I didn't find the type_traits self contained alternative for mpl::if_c.

Rani


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