Thread: template metaprogramming

  1. #1
    @codeguru
    Join Date
    Jan 2006
    Posts
    34

    template metaprogramming

    hey evryone im new to the forums and i have a very good question.
    I think im getting good at C++ now, but still learning. i want to make a multiple dimension array using template metaprogramming i have made my own array class and now i wud like to make a dimensional array class. I know how template meta programming works but i still have no idea how to implent it so i can make a dimensional array.
    can someone giveme some easy example code todo this without using any other classes so i can see what your doing.

    thanks alot, mitsukai

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Template metaprogramming is never easy.

    Anyway. Here's a small template metaprogram that nests n vectors. (Requires partial template specialization.)
    Code:
    #include <vector>
    
    // Recursive definition.
    template<typename T, int N>
    struct n_vector
    {
      typedef std::vector< typename n_vector<T, N-1>::type > type;
    };
    
    // Termination clause.
    template<typename T>
    struct n_vector<T, 1>
    {
      typedef std::vector<T> type;
    };
    Usage:
    Code:
    typedef typename n_vector<int, 3>::type my_vector;
    // is equivalent to
    // typedef std::vector< std::vector< std::vector< int > > > my_vector;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    i created another problem wich i have no idea how to do.

    Code:
    template<typename t_T, unsigned long t_D>
    class DArray : public Array< DArray<t_T, t_D - 1> > 
    {
    public:
    	class DArray(const unsigned long& s, ...)
    	{
    		va_list val;
    		va_start(val, s);
    
    		unsigned long x;
    		Array* y = *this;
    		for(x = 1; x <= t_D; ++x)
    		{
    			y.Size = va_arg(val, unsigned long);
    			y = *y.p_Array[0];
    			//somehow initialize evry dimension to size of next arg
    		}
    
    		va_end(val);
    	}
    };
    
    template<typename t_T>
    class DArray<t_T, 1> : public Array<t_T>
    {
    };
    i want it so i can use DArray<long, 2> x(5, 5); that will create a DArray 2 dimension with the size of 5 x 5, i think this is a very tough algorythm, i tried it but my mind is very limited to these such things, untill i actually learned how todo it... i cant think of anything that can do this. maybe some kind of dimension class that will initialize the needed dimensions...

  4. #4
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    help

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Show the definition of Array.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    Code:
    template<typename t_T>
    class Array

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's not the definition, just the declaration.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    pmed
    Last edited by Mitsukai; 01-20-2006 at 05:53 AM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Uh ... no. No, I don't think I fancy diving into that. Sorry.

    Just a hint, I think Array is about 10 times more complicated than it needs to be.

    And stop PMing me.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM