Sign in to follow this  
snk_kid

Just for fun....

Recommended Posts

In another forum we where talking about multidimensional vectors specifically 2D vector, i got bored and thought i wonder if we can have a std::vector of 100th dimension and i came-up with this meta-template program, see if you can understand it and if your compiler can handle this:
#include <cstddef>
#include <vector>

template < typename T, size_t N, template < typename U, typename W > class con,  template < typename G > class Alloc = std::allocator >
struct gen_multi {


   typedef con< T, Alloc<T> > simple;

   typedef typename gen_multi< simple,  N - 1, con, Alloc >::simple n_container;


};

template < typename T, template < typename U, typename W > class con,  template < typename G > class Alloc >
struct gen_multi< T, 0, con, Alloc  > {

   typedef con<T, Alloc<T> > simple;

   typedef simple n_container;


};

int main() {


   gen_multi< int, 100, std::vector >::n_container foo;

   return 0;

}
after about 10 mins i get this output from GCC 3.4.2:
cc1plus.exe: out of memory allocating 297795646 bytes
LOL so i kept on changing the size and managed to get it to compile at N == 20 so a 20D vector lol:
int main() {


   gen_multi< int, 20, std::vector >::n_container foo;

   return 0;

}
See if you can get it any higher! [Edited by - snk_kid on September 30, 2004 7:43:27 PM]

Share this post


Link to post
Share on other sites
i should note that your code is wrong, btw.
your template signature for the container, and the template signature for vector do not match.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
i should note that your code is wrong, btw.
your template signature for the container, and the template signature for vector do not match.


note taken and code updated, some reason gcc allowed it.

Share this post


Link to post
Share on other sites
Just tested it on VC++ 7.1, surprisingly it allowed N == 937 and it was pretty quick.

[Edited by - snk_kid on September 30, 2004 8:28:46 PM]

Share this post


Link to post
Share on other sites
Yeah, vc7.1 uses a series of dynamically growing heaps when compiling. you can set a limit on it with the /Zm flag.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Yeah, vc7.1 uses a series of dynamically growing heaps when compiling. you can set a limit on it with the /Zm flag.


Actually i think i made a mistake because when i went to test it out on gcc again it wouldn't compile so the only way it would work on both is by making type Alloc a template template parameter then VC++ 7.1 only allowed upto 9 levels of recusions (with-out setting flags) while gcc upto 20.

I've updated the code again.

Share this post


Link to post
Share on other sites
Next thing you know you'll be doing a fully flegged matrix multiplication template expansion.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Just an observation that snk_kid now has a 1337 rating. w00t.


Dang it! I planning to post the same thing and then I saw this. Nobody ever rate snk_kid again so he can be 1337 forever! :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this