#include <cstdio>#include <cstdlib>template <template<unsigned> class F, unsigned N>class value_array{ template <template<unsigned> class F, unsigned N> class value_holder { value_holder<F,N-1> base; const unsigned val; public: value_holder(void): val( F<N>::val){} }; template <template<unsigned> class F> class value_holder<F,0> { const unsigned val; public: value_holder(void): val( F<0>::val){} }; static const value_holder<F,N> values;public: static const unsigned size = N; unsigned operator[](unsigned n){ return *(reinterpret_cast<const unsigned*>( &values)+n);}};template <template<unsigned> class F, unsigned N>const typename value_array<F,N>::value_holder<F,N> value_array<F,N>::values;//need a test "function"template <unsigned N>struct fib{ enum {val = fib<N-1>::val + fib<N-2>::val};};template <>struct fib<0>{ enum {val = 1};};template <>struct fib<1>{ enum {val = 1};};int main(void){ static value_array<fib, 10> fibo; for(unsigned n = 0; n != 10; ++n) printf("%d ", fibo[n]); puts(""); system("pause");}
looking at the asm output for it you can find this gem
_DATA SEGMENT?values@?$value_array@Ufib@@$09@@0V?$value_holder@Ufib@@$09@1@B DD 01H ; value_array<fib,10>::values DD 01H DD 02H DD 03H DD 05H DD 08H DD 0dH DD 015H DD 022H DD 037H DD 059H_DATA ENDS
that's compile values =)
and yes, Im insane...