0

I am trying to use compile time recursion to output the elements of a grid. This is mainly just to try to become familiar with what this technique can do.

As an example, I am trying to output the values of a grid (a vector> ). Here is what I have so far:

#include <iostream>
#include <vector>

#include <boost/utility/enable_if.hpp>

typedef std::vector<std::vector<int> > GridType;

template <std::size_t GridDimensions>
struct TestClass
{
  template <std::size_t Dim>
  typename boost::enable_if_c< (GridDimensions == Dim),
  void >::type loop(GridType& grid) { };

  template <std::size_t Dim>
  typename boost::disable_if_c< (GridDimensions == Dim),
  void >::type loop(GridType& grid)
  {
    for(std::size_t i = 0; i < grid.size(); ++i)
    {
      // loop in the nested dimension:
      loop< Dim + 1 >(grid);
    };

  }; // end loop()

}; // end TestClass

int main(int argc, char *argv[])
{
  std::vector<std::vector<int> > values;
  for(unsigned int i = 0; i < 10; ++i)
    {
    std::vector<int> vec;
    for(unsigned int j = 0; j < 5; ++j)
      {
      vec.push_back(j);
      }
    values.push_back(vec);
    }
  TestClass<2> test;
  test.loop<0>(values);
  return 0;
}

However, I'm not sure where to put the output statement, or what the for loop inside loop() should loop over (I'd think the size of the inner arrays?).

The idea is supposed to be that you'd only need to implement an output loop over a single row, and the code for that row would be generated for every row in the grid, right?

David Doria
  • 9,873
  • 17
  • 85
  • 147
  • are you sure you know what you're trying to do? you wan't to apply compile-time recursion to display a grid, which you fill at run-time? It's not like C++ meta-programming works. Take a look at other such questions on SO, e.g. http://stackoverflow.com/q/5274149/106688 – Andrey Jan 30 '12 at 01:36

1 Answers1

0

Here's what I think you're trying to do:

#include <iostream>
#include <vector>

#include <boost/utility/enable_if.hpp>

typedef std::vector<std::vector<int> > GridType;

template <std::size_t GD>
struct TestClass
{
    template<typename GT>
    static void render(GT& grid)
    {
        for(std::size_t i = 0; i < grid.size(); ++i)
        {
            // loop in the nested dimension:
            TestClass<GD - 1>::render(grid[i]);
        };

    };

}; // end TestClass

template <>
struct TestClass<0>
{
    template<typename GT>
    static void render(GT& grid)
    {
        for(std::size_t i = 0; i < grid.size(); ++i)
        {
            std::cout << i << " : " << grid[i] << std::endl;
        };

    };

}; // end TestClass

int main(int argc, char *argv[])
{
    std::vector<std::vector<int> > values;
    for(unsigned int i = 0; i < 10; ++i)
    {
        std::vector<int> vec;
        for(unsigned int j = 0; j < 5; ++j)
        {
            vec.push_back(10 * j + i);
        }
        values.push_back(vec);
    }

    TestClass<1>::render(values);

    return 0;
}

It helps with this type of TMP to work out what the non-templated code might look like first.

jon hanson
  • 8,722
  • 2
  • 37
  • 61