|
Boost : |
From: Daniel James (daniel_at_[hidden])
Date: 2004-09-14 09:29:17
David Abrahams wrote:
> "Arkadiy Vertleyb" <vertleyb_at_[hidden]> writes:
>
>
>>templates, and still set the compile-time variables. As far as I
>
> ^^^^^^^^^^^^^^^^^^^^^^
>
> Excuse me, but what the heck does that mean? As far as I know,
> there's no mutable data at compile-time.
>
As far as I understand it, by creating a type like:
template<int N> struct counter : counter<N - 1> {};
template<> struct counter<0> {};
you can create a counter by overloading on increasing values of N. So,
you first create a function:
size_type<0>::type check(...);
And sizeof(check(counter<MAX_VALUE>)) == 0. Then overload it with:
size_type<1>::type check(counter<1>);
And sizeof(check(counter<MAX_VALUE>)) == 1. And so on.
You can then overload another function based on the current value of
counter to get a 'variable'. Daniel Wallin's implementation is at:
http://lists.boost.org/MailArchives/boost/msg69842.php
He adds an extra type parameter to get multiple counters. The function
overloads are generated by friend functions of a templated class, which
is probably not standard compliant - it doesn't work in strict mode on
EDG compilers. Goran Mitrovic's version declares the functions in
macros, so it works in strict mode, but this means you can't use it from
inside classes, so it's no use here. He posted it at:
http://lists.boost.org/MailArchives/boost/msg69850.php
In Peder's code the name isn't really appropriate as most of the data is
actually constant, the only actual variable is the counter is used to
generate a unique index for each encoded type. Although all the data is
stored using these function overloads.
Daniel
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk