Boost logo

Boost :

From: Daniel James (daniel_at_[hidden])
Date: 2004-08-28 06:47:23


I've come up with an alternative way to invoke the typeof emulations
that have been posted. It basically works by defining an iterator over
the encoded type. Unfortunately, it can't be used inside a function, and
is awkward to use in general, but has the advantage is that it only has
to define the call to foo once, which might result in faster compile times.

It could possibly also remove the limit on the complexity of the type.
Arkadiy's version would require a some changes to do this, but I think
Peder's could quite easily. Although I'm not sure as I've only had a
brief look at his version.

I've implemented it as a sort of typedef, which seems like the easiest
way to use it.

It's probably easier to understand the code than my explanation, so here
it is. It's based on Arkadiy's version in the yahoo groups file section.

     #define BOOST_TYPEOF_TYPEDEF_ITERATOR_NAME(Name) \
         BOOST_PP_CAT(BOOST_TYPEOF_TYPEDEF_ITERATOR_##Name##_, __LINE__)

     #define BOOST_TYPEOF_TYPEDEF_ITERATOR(Expr, Name) \
         struct BOOST_TYPEOF_TYPEDEF_ITERATOR_NAME(Name) { \
             template <int pos> \
             struct iterator { \
                 typedef boost::mpl::int_< \
                     sizeof(boost::type_of::foo<pos>(Expr))> type; \
                 typedef iterator<pos+1> next; \
             }; \
         };

     #define BOOST_TYPEOF_TYPEDEF(Expr, Name) \
         BOOST_TYPEOF_TYPEDEF_ITERATOR(Expr, Name) \
         typedef boost::type_of::decode_type< \
             BOOST_TYPEOF_TYPEDEF_ITERATOR_NAME(Name)::iterator<0> \
>::type Name;

     #define BOOST_TYPEOF_TPL_TYPEDEF(Expr, Name) \
         BOOST_TYPEOF_TYPEDEF_ITERATOR(Expr, Name) \
         typedef typename boost::type_of::decode_type< \
             BOOST_TYPEOF_TYPEDEF_ITERATOR_NAME(Name)::iterator<0> \
>::type Name;

Here is an example of it's use:

     template <class Op>
     struct result_of_nullary_functor
     {
         static Op op;
         BOOST_TYPEOF_TPL_TYPEDEF(op(), type);
     };

     struct g { int operator()(); };
     BOOST_MPL_ASSERT_IS_SAME(result_of_nullary_functor<g>::type, int);

I've only tested on the intel linux compiler.

Daniel


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