|
Boost : |
Subject: Re: [boost] [typeof] Determining if a type has been registered or not at compile time
From: Peder Holt (peder.holt_at_[hidden])
Date: 2009-12-25 07:25:18
2009/12/22 Edward Diener <eldiener_at_[hidden]>
> Is there any way one can determine whether a type has been registered or
> not with Boost.Typeof at compile time so that the information can be used in
> TMP ? I am thinking, of course, of the use of typeof in a template, where a
> template parameter may or may not be aregistered type when the template has
> been instantiated. I would like to provide my own TMP fallback code if the
> type has not been registered rather than see an immediate compiler error
> generated. Something like:
>
> Here is a possible implementation of boost::typeof::is_registered:
namespace boost { namespace type_of {
class AllTypesRegisteredVector {};
class NotAllTypesRegisteredVector {};
template<typename V>
struct vector_handler {
template<typename Type_Not_Registered_With_Typeof_System>
struct handle_unregistered_type;
};
template<>
struct vector_handler<AllTypesRegisteredVector> {
template<typename T>
struct handle_unregistered_type {
typedef NotAllTypesRegisteredVector type;
};
};
template<>
struct vector_handler<NotAllTypesRegisteredVector> {
template<typename T>
struct handle_unregistered_type {
typedef NotAllTypesRegisteredVector type;
};
};
template<class T>
struct push_back<NotAllTypesRegisteredVector,T> {
typedef NotAllTypesRegisteredVector type;
};
template<class T>
struct push_back<AllTypesRegisteredVector,T> {
typedef AllTypesRegisteredVector type;
};
}}
BOOST_TYPEOF_BEGIN_ENCODE_NS
template<class V,class T>
struct encode_type_impl
{
typedef typename
boost::type_of::vector_handler<V>::handle_unregistered_type<T>::type type;
};
BOOST_TYPEOF_END_ENCODE_NS
namespace boost {namespace type_of {
template<typename T>
struct is_registered : boost::is_same<AllTypesRegisteredVector,typename
encode_type<AllTypesRegisteredVector,T>::type> {};
}}
> boost::mpl::eval_if
> <
> boost::typeof::registered<T>, // this would be nice to have
> boost::mpl::identity<BOOST_TYPEOF_TPL(some_expression)>,
> boost::mpl::identity</* use another facility to get the type I want into a
> metafunction*/>
> >
>
>
The problem is that BOOST_TYPEOF_TPL is evaluated even if is_registered is
false.
One way to workaroun this is to allow the user to add a default type:
typedef
BOOST_TYPEOF_WITH_DEFAULT(some_expression,some_backup_type_if_some_expression_not_registered)
type;
Is this acceptable?
_______________________________________________
> 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