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

  • SEARCH
  • Home
  • 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 7012929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:19:12+00:00 2026-05-27T22:19:12+00:00

I was reading the responses to Printing 1 to 1000 without loop or conditionals

  • 0

I was reading the responses to “Printing 1 to 1000 without loop or conditionals” and I am wondering why it is necessary to have the special case for NumberGeneration<1> in the top answer.

If I remove that and add a check for N == 1 in the template (code below), the code fails compilation with “template instantiation depth exceeds maximum” but I’m not sure why. Are conditionals handled differently in compile-time?

#include <iostream>

template<int N>
struct NumberGeneration
{
    static void out(std::ostream& os)
    {
        if (N == 1)
        {
            os << 1 << std::endl;
        }
        else
        {
            NumberGeneration<N-1>::out(os);
            os << N << std::endl;
        }
    }
};

int main()
{
    NumberGeneration<1000>::out(std::cout);
}
  • 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-27T22:19:12+00:00Added an answer on May 27, 2026 at 10:19 pm

    Code generation and compilation doesn’t branch depending on conditionals! Consider this:

    // don't declare bar()!
    
    void foo()
    {
         if (false) { bar(); }
    }
    

    If you never declare bar(), this is a compilation error even though the inner scope can never be reached. For the same reason, NumberGeneration<N-1> is always instantiated, no matter whether that branch can be reached or not, and you have infinite recursion.

    Indeed, the static analogue of conditionals is precisely template specialization:

    template <> struct NumberGeneration<0> { /* no more recursion here */ };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

after reading the responses I have rewritten my question. Let's say I have a
I have a few simple questions, because I got confused reading all difference responses.
Edit: After reading the responses, I believe the answer is don't do this, hence
Reading over the responses to this question Disadvantages of Test Driven Development? I got
According to the responses in Why subtract null pointer in offsetof()? (and my reading
Reading through this question on multi-threaded javascript, I was wondering if there would be
I have been reading a lot about MVC these days and I think I
Why use precompiled headers? Reading the responses, I suspect what I've been doing with
After reading over some of the other responses to this type of question I
SOLVED: Per the two responses about having a fixed size, i do have a

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.