Just for fun....

Started by
9 comments, last by Zahlman 19 years, 6 months ago
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]
Advertisement
Template recursion. Simple really.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

i should note that your code is wrong, btw.
your template signature for the container, and the template signature for vector do not match.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.
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]
Yeah, vc7.1 uses a series of dynamically growing heaps when compiling. you can set a limit on it with the /Zm flag.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.
Next thing you know you'll be doing a fully flegged matrix multiplication template expansion.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Just an observation that snk_kid now has a 1337 rating. w00t.
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

This topic is closed to new replies.

Advertisement