Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-04-20 08:17:56

"Arkadiy Vertleyb" <vertleyb_at_[hidden]> writes:

> About one and a half year ago Steve Dewhurst published three articles on
> this subject in CUJ. The idea was to encode the type as an integer using
> bitwise operations. This integer could be than "returned" from the
> function, this crossing the boundaries between a type and a value, and then
> decoded into the original type.

That's the basic idea behind all typeof() emulations that I've ever
seen. They all require some type registration to associate a
compile-time integral constant with a type. The innovation that Steve
brought, IIUC, was that with the registration of only class types, the
rest of the transformations possible in the type system could be
encoded by the library automatically. For example, if you register
class Foo, then the library can generate encodings for Foo*, Foo[2],
Foo**, int (Foo), Foo(*)(Foo,Foo), etc., so you don't have to register
those types. The problem is that those numbers can get very large...

> I wonder if this or any other technique is currently being
> considered by anyone to be added to Boost or we are just waiting
> until the real typeof is available?
> I have been experimenting with this, and came up with a very simple
> trick that allows to use a static list of integers, like
> mpl::vector_c, instead of bitwise operations, to encode the type, that you need multiple-precision integers to represent the
values. What Steve did IIRC was to use array types, where each array
bound encoded an additional base 2^32 "digit", so


would endcode

    3 * (2^32)^0 + 26 * (2^32)^1

I think I have that right ;-)

My guess is that manipulating array types in that way has a lower
cost at compile-time than using a vector_c, though using mpl probably
yields much nicer library code and vector_c is more portable.

You might look at this thread ("typeof()
substitute) to see how Aleksey quickly built a sequence iterator for
array types.

It might be interesting to get your typeof implementation together
with what Joel describes in

> and pass this list across the function boundaries. I think, if
> nothing else, this is much simpler than bitwise operations.
> Everything else is pretty much like in Steve's approach. Based on
> this it seems to be pretty simple to encode/decode types to
> implement typeof-like facilities for some specific (or maybe not so
> specific) cases.

For what it's worth, I'm pretty sure I know how to solve the problem
that most typeof() implementations strip top-level references. See
boost/iterator/is_lvalue_iterator.hpp for a demonstration.


Dave Abrahams
Boost Consulting

Boost list run by bdawes at, gregod at, cpdaniel at, john at