|
Boost : |
From: Peder Holt (peder.holt_at_[hidden])
Date: 2004-08-31 03:58:14
I think i am closing in on the ultimate typeof implementation.
Given the maximum depth of an expression = N (BOOST_MPL_LIMIT_VECTOR_SIZE)
and the depth of the expression we are evaluating = m
My earlier implementation was of the order (N^2)
Arkadiys implementation (if I am correct) is of the order (m*N)
My new implementation is of the order (m) !!
Meaning it is linear. I only need to evaluate an expression once, and
if the expression is simple, very few templates need to be
instantiated.
There is only the compiler limit to the complexity of an expression.
Here is the define for BOOST_TYPEOF:
#define BOOST_TYPEOF(expr) \
boost::type_of::decode<\
boost::type_of::value_iterator<\
sizeof(boost::type_of::encode_start(expr))\
>\
>::type
I think this is about as simple as it can get without the real thing.
encode_start(expr) does not return the size of an item in the
expression. It returns the starting index of an iterator.
Every level in the expression instantiates a template class. Every
template class instantiates two friend functions. The first function:
encode_value() takes the current index as input argument, and returns
the encoded value of the current level.
The other function:
encode_index() incremets the current index count. (Up to a maximum of
BOOST_MAX_TYPEOF_COUNTER, defaulted to 1000)
This is the maximum number of levels that can be encoded in a single c++ file.
To access these values:
sizeof(boost::type_of::encode_value((boost::type_of::encode_counter<N>*)0)
returns the value at iterator N.
(sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<BOOST_MAX_TYPEOF_COUNTER>*)0)))
returns the next available index
These are defined as BOOST_TYPEOF_VALUE(N)
and BOOST_TYPEOF_INDEX()
respectively
I tested my 120 level deep type with very little overhead in compile time.
Since I rely heavily on the order of template instantiation, it may be
fragile to compiler errors (I am compiling this on VC 6.5)
I have had some problems with ETI screwing up the scheme when typeof
is used inside templates, but this is workaroundable by being more
relaxed on the indexing.
The source can be found at:
http://groups.yahoo.com/group/boost/files/typeof_fast.zip
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk