From: Arkadiy Vertleyb (vertleyb_at_[hidden])
Date: 2004-08-29 18:42:22
"Goran Mitrovic" <gmit_at_[hidden]> wrote
> Sorry to intrude, but, would it be possible for you to:
> - explain a bit algorithm on which your typeof works
1) The expression is passed into the function template foo(). Inside this
function the required type is known.
2) To pass the type across the function boundaries, it is encoded into a
list of integers.
3) Every item in the list gets passed across function boundaries
individually by the means of sizeof().
4) The list of integers gets decoded into the original type.
To encode a type into a list of integers, partial template specializations
are used (this is fundamental difference of my approach from one suggested
by Peder). These specializations are defined mainly by the means of two
macros: REGISTER_TYPE and REGISTER_TEMPLATE. Every type and template that
participates in the expression needs to be registered. Every type/template
is assigned a unique integer identifier. A type is encoded by a single
integer (its identifier). A template is encoded by its identifier plus
recursively encoding its parameters.
A similar approach is employed for pointers, references, function pointers,
etc., with the difference that the specializations are specified directly,
by the library.
> - explain why is your algorithm so slow - on my Athlon 2200 and VS7.1, it
> takes about a second per typeof too be processed
First of all, it can't be fast, by definition.
Just think what's happenning here... The compiler already knows the type of
the expression, but it wouldn't tell us. If it would, there would be no
problem in the first place. So we are trying to find out indirectly, and to
do this we instantiate a couple of hundreds of templates... The template
metaprogramming is done by the means of template instantiation -- I don't
know any other way -- if you do, please tell me. And template instantiation
is not such a cheap thing.
Barring this, there is an internal ineficiency in my algorithm, and I
already explained it in one of my previous post. The problem is I am always
passing a pre-defined number of integers, namely BOOST_MPL_LIMIT_VECTOR_SIZE
of them, even if the type is simple enough to be encoded just with a couple
The reason of this is that the real size of the vector becomes known at
compile time. To be able to operate this compile-time constant, I need to
pass it to a template meta-function. But it's impossible to also pass the
expression (the original parameter of typeof ) to a metafunction.
Therefore, the only way to do it is to create metafunction for this
particular expression, by the means of preprocessor, at the call site. And
this introduces a few very nasty (IMO) things, such as class naming issues,
limitation of contexts in which typeof can be used, etc. Some people may
consider these issues acceptable, for me it's a showstopper.
> which is, in my opinion,
> quite unacceptable.
> What is the speed bottleneck and is there a way to strip
> some features (function pointers, for example) to fasten compilation?
These features just provide additional template specializations, and IMO
should not noticably affect the compilation speed. If you types are simple,
reduce BOOST_MPL_LIMIT_VECTOR_SIZE -- it will get faster.
> the implementation
> is too messy
Would you mind to elaborarate on this please -- I find this statement quite
More importantly, I just find it not true.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk