Boost logo

Boost :

From: Rani Sharoni (rani_sharoni_at_[hidden])
Date: 2002-12-17 13:44:30


>"Daniel Frey" <daniel.frey_at_[hidden]> wrote in message
news:3DFF424B.E340F394_at_aixigo.de...
>Rani Sharoni wrote:
>>
>> In the same fashion - I think that if type is not cv void, not reference
and
>> not convertible to bool (via standard conversion) then it's class type.

>Hm, could you show an implementation? [...]
Sure.

//
// Please implement using boost facilities
// is_reference, is_void, yes_type, no_type
// and BOOST_STATIC_ASSERT
//
template<typename T>
struct is_class
{
private:
    typedef char (&yes)[1];
    typedef char (&no) [2];

    struct boolcvt { boolcvt(bool); };

    static no check(boolcvt);
    static yes check(...);

    static T& getT();
public:
    static const bool result =
        sizeof(check(getT())) == sizeof(yes);
};

template<typename T>
struct is_class<T&> { static const bool result = false; };

// add cv void versions
template<>
struct is_class<void> { static const bool result = false; };

// needed ? (EDG doesn't but VC7.1 does)
template<typename T>
struct is_class<T[]> { static const bool result = false; };

struct A { operator bool(); };
union U {};
enum E {};

typedef int test[is_class<A>::result];
typedef int test[is_class<U>::result];
typedef int test[not is_class<E>::result];
typedef int test[not is_class<A(int,A)>::result];
typedef int test[not is_class<A&>::result];
typedef int test[not is_class<A[]>::result];

void f() {
    struct pitfall {};
    // is_class<pitfall>::result;
}

worked with VC7.1 and Comeau C/C++ 4.3.0.1

>> It might be useful implementation for compliers on which the SFIAE
technique
>> to detect class type doesn't work.

>What is SFIAE? [...]
I'm sorry, it should be SFINAE - substitution failure is not an error (but
elimination of function template or partial specialization from candidate
set).

> template< typename > no_type test( ... );
> template< typename T > yes_type test( void (T::*)() );

> stuff from Paul Mensonides?
Exactly.

Enjoy,
Rani


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