Boost logo

Boost :

From: John Maddock (John_Maddock_at_[hidden])
Date: 2002-02-23 07:27:00


> In my use of type_traits, I've frequently wanted to be able to turn
> boolean
> values into tags for tag dispatching, e.g.:
>
> return dispatch_function(&x, boost::is_const<X>::type());
>
> As it stands, I can write:
>
> template <bool> struct bool_t {};
> return dispatch_function(&x, bool_t<boost::is_const<X>::value>());
>
> But, it's a little cumbersome, and nearly all of type_traits ICEs are
> expressed in terms of yes_type and no_type anyway. Any chance of
> institutionalizing this and making it public?

There are no internal types that can be just exposed, however there is an
overlooked section in my type_traits proposal which can come to the rescue
here:

"There is one potential problem with integral constant members that should
be raised: these require an out of line definition as well as an inline
initialisation (but see issues 48 and 82). One implementation strategy that
avoids the problem of having to provide lots of definitions for these
static members, would be to have the traits classes inherit from a common
base class:

template <bool b> struct boolian_traits{ static const bool value = b; };
template <bool b> bool boolian_traits<b>::value;

typedef boolian_traits<true> true_type;
typedef boolian_traits<false> false_type;

template <class T> struct is_void : public false_type {};
template <> struct is_void<void> : public true_type {};
// etcThis does not form part of this proposal, and this particular issue
is ignored in the boost library, however there may be sufficient merit to
this scheme to make it worth while adding to the proposed text. This scheme
also makes it trivial to select a function overload based on whether a
traits class inherits from true_type or false_type."

To flesh this out some more - if the traits classes all inherit from either
true_type or false_type, then we can just write something like:

template <class T>
inline void do_destroy(T* p, const false_type&)
{
   p->~T();
}

inline void do_destroy(void*, const true_type&)
{}

template <class T>
inline void destroy(T* p)
{
  do_destroy(p, has_trivial_destructor<T>());
}

I don't have time to do any more with this right now, but if you want to
run with it do :-)

- John Maddock
http://ourworld.compuserve.com/homepages/john_maddock/


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