Boost logo

Boost :

From: John Maddock (jm_at_[hidden])
Date: 2002-10-27 07:03:06


Sorry to take so long to look into this, I've been experimenting with
several compilers (including one that I think we both have, but can't talk
about), but haven't been able to get this implementation working (not even
if they accept the is_class implementation). All complain that you're
trying to derive from something that isn't a class type, and I think they're
right as well. It seems to me that is_union_inherit<T> must be instantiated
no matter which template overload is selected, since it's part of the
expression that determines which overload to use, rather than part of the
function signature.

John Maddock
http://ourworld.compuserve.com/homepages/john_maddock/index.htm

> Presented below:
>
> #include "boost/static_assert.hpp"
> #include "boost/config.hpp"
>
> typedef char (&yes_tag)[1];
> typedef char (&no_tag)[2];
>
> template< typename T > yes_tag is_class_helper(void (T::*)(void));
> template< typename T > no_tag is_class_helper(...);
>
> template < typename T > struct is_union_inherit : T {};
> template< typename T > yes_tag is_union_class_helper(T*);
> template< typename T > no_tag is_union_class_helper(...);
>
> template< typename T >
> struct is_union
> {
> BOOST_STATIC_CONSTANT(bool
> , value =
> sizeof(is_union_class_helper<T>(
> static_cast< is_union_inherit<T>* >(0)
> )) == sizeof(no_tag)
> &&
> sizeof(is_class_helper<T>(0)) == sizeof(yes_tag)
> );
> };
>
> union U {};
> class C {};
> struct S {};
>
> BOOST_STATIC_ASSERT(is_union<U>::value);
> BOOST_STATIC_ASSERT(!is_union<C>::value);
> BOOST_STATIC_ASSERT(!is_union<S>::value);
> BOOST_STATIC_ASSERT(!is_union<int>::value);
>
> Works with Intel C++ 6.0. The only catch is that it doesn't work with
> incomplete types.
>
> On a related note, it seems to me that the current semantics of
> 'boost::is_class' trait:
>
> <docs quote>
> "Evaluates to true only if T is of class/struct type."
> </>
>
> does not match the standard - section 9 clearly states that union is a
class
> too.
>
> Aleksey
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
>


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