|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2004-09-16 09:51:53
Peder Holt <peder.holt_at_[hidden]> writes:
> I am not familiar with the full set of ADL rules, so I'll examplify instead.
> I'll list the most important concepts I rely on:
>
> As Daniel pointed out, the following construct is rather essential:
>
> template<int N> struct encode_counter : encode_counter<N - 1> {};
> template<> struct encode_counter<1> {};
>
> The template recursion should be reasonably large (eg. 64 or above)
> This limits the number of typeofs that can be used per compilation units
>
> This limit is not absolute. I have a method of getting around this limit.
>
> template<unsigned N>
> struct sizer {
> BOOST_STATIC_CONSTANT(unsigned,value=N);
> typedef char(&type)[value];
> };
>
> sizer<1>::type encode_value(...);
> sizer<1>::type encode_index(...);
>
> #define BOOST_TYPEOF_INDEX()
> (sizeof(encode_index((encode_counter<MAX_RECURSION_DEPTH>*)0)))
>
> template<typename T>
> struct encode_type {
> BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX());
>
> BOOST_STATIC_CONSTANT(unsigned,next=value+1);
> friend sizer<next> encode_index(encode_counter<next>*);
> sizer<value>::type resize;
> };
>
> template<typename T>
> encode_type<T> encode_start(T const&);
>
> void main() {
> int index_before=BOOST_TYPEOF_INDEX() //should return 1
> double* a;
> int index=sizeof(encode_start(a)); //should install a new
> encode_index function
> int index_after=BOOST_TYPEOF_INDEX() //should return 2, hence it
> must find the encode_index function embedded in the encode_type
> struct.
> }
>
> That is, Instantiating a template class with a friend function, should
> expose this function uncritically.
That's nonconforming, but works in vc6 (and most other major
compilers).
> In addition to the above requirements, I expect the compiler to
> recursively instantiate template types, as in the dummy-example below:
>
> template<typename T,unsigned Index,unsigned Position>
> struct dummy_encode2 {
> sizer<DOUBLE_ID>::type encode_value(sizer<Index>
> const*,sizer<Position>*);
> };
>
> template<typename T,unsigned Index,unsigned Position=1>
> struct dummy_encode1 {
> BOOST_STATIC_CONSTANT(unsigned,instantiate=sizeof(dummy_encode2<T,Index,Position+1>));
> sizer<POINTER_ID>::type encode_value(sizer<Index>
> const*,sizer<Position>*);
> };
>
> Then the expression
> sizeof(dummy_encode1<double*,1>) should install two encode_value functions.
Sorry, I don't see any recursion here.
-- 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