Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7219091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:34:59+00:00 2026-05-28T21:34:59+00:00

I am trying to use compile time recursion to output the elements of a

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:35:04+00:00Added an answer on May 28, 2026 at 9:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Spring's @AspectJ compile-time weaving instead of <aop:autoproxy/> and it
I am trying to use the aspectj-maven-plugin in a maven project. At compile time,
I'm trying to use aspectj with compile time weaving to support annotations like Spring's
I am trying to use MinGW to compile a C program under Windows XP.
I'm trying to use the this keyword in a static method, but the compiler
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
I'm trying to use NetBeans to compile C code and have the following versions
I've encountered a strange issue using CompiledQuery.Compile . When trying to use a static
This is the provided function template I'm trying to use: template <class Process, class
I'm getting this error when I run my app (not at compile time): Could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.