Boost logo

Boost :

Subject: Re: [boost] [Boost-users] [non Boost] Template troubles.
From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2008-12-16 16:50:50


Robert Jones wrote:
> On Fri, Dec 12, 2008 at 5:52 PM, Noah Roberts <roberts.noah_at_[hidden]>wrote:
>
>> Noah Roberts wrote:
>>
>
> Neil, Noah, Kim - Thanks all for your input. Although no perfect
> solution has emerged it is always good to see a few good minds
> looking at it.

Don't recall if it was said already but you can also explicitly say
which type you're using:

int main( )
{
   Block< int, 16 > :: type a;
   const Block< int, 16 > :: type b = { };

   assign<int[16]>( a, b );
}

Then you'll need to establish template metaprograms to remove one array
level from a type and get the type beneath in order to write your
recursive template call so that it can cast the next level:

template < typename T >
struct next_level
{
   typedef T type;
};

template < typename T, size_t sz >
struct next_level<T[sz]>
{
   typedef T type;
};

template < typename T >
   void assign( T & assignee, const T & value )
{
   assignee = value;
}
template < typename T, std :: size_t sz >
   void assign( T ( & assignee )[ sz ], const T ( & value )[ sz ] )
{
   for (size_t i = 0; i < sz; ++i)
     assign<typename next_level<T>::type>(assignee[i], value[i]);
}

Tested and works in msvc:

int main( )
{
   typedef Block<int,3>::type type_t;
   type_t a;
   type_t const b = { { 1,2,3}, {4,5,6}, {7,8,9} };

   assign<int[3]>( a, b );

   for (int i = 0; i < 3; ++i)
   {
     for (int j = 0; j < 3; ++j)
       std::cout << a[i][j] << " ";
     std::cout << std::endl;
   }

   std::cin.get();
}


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