Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-09-14 10:24:48


Daniel James <daniel_at_[hidden]> writes:

> 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.

Hah. Relies on ODR violation.

But we agreed that was okay for Arkadiy's implementation too, as long
as it actually worked, because real typeof is coming.

As usual, I worry about what happens on compilers with link-time
instantiation.

> 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.

Right. Boost.Python used to do that. Came as a terrible shock when
I discovered it can't work.

> 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

Because the declaration disappears when the class scope is exited and
the counter will be reused?

> 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.

Thanks for the explanation.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

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