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 7402419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:44:13+00:00 2026-05-29T04:44:13+00:00

I have difficulties to understand how the following template definition and template specialization definition

  • 0

I have difficulties to understand how the following template definition and template specialization definition work? To me, factorial<34> or factorial<T-1> look strange!

For example:

factorial<T - 1>::value

means what?

#include <iostream>

template<int T>
struct factorial {
  enum { value = factorial<T - 1>::value * T };
};

template<>
struct factorial<1> {
  enum { value = 1 };
};

int main()
{
  std::cout << factorial<34>::value << std::endl;

}    

g++ -o testSTL01 testSTL01.cpp -Wall
testSTL01.cpp: In instantiation of ‘factorial<13>’:
testSTL01.cpp:5:3:   instantiated from ‘factorial<14>’
testSTL01.cpp:5:3:   instantiated from ‘factorial<15>’
testSTL01.cpp:5:3:   instantiated from ‘factorial<16>’
testSTL01.cpp:5:3:   instantiated from ‘factorial<17>’
testSTL01.cpp:5:3:   instantiated from ‘factorial<18>’
testSTL01.cpp:5:3:   [ skipping 11 instantiation contexts ]
testSTL01.cpp:5:3:   instantiated from ‘factorial<30>’
testSTL01.cpp:5:3:   instantiated from ‘factorial<31>’
testSTL01.cpp:5:3:   instantiated from ‘factorial<32>’
testSTL01.cpp:5:3:   instantiated from ‘factorial<33>’
testSTL01.cpp:5:3:   instantiated from ‘factorial<34>’
testSTL01.cpp:15:29:   instantiated from here
testSTL01.cpp:5:3: warning: integer overflow in expression
start to run the app ...

0
  • 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-29T04:44:14+00:00Added an answer on May 29, 2026 at 4:44 am

    This is an example of template metaprogramming. This program calculates factorial at compile time using recursion. The base of recursion is here:

    template<>
    struct factorial<1> {
      enum { value = 1 };
    };
    

    It says that factorial of 1 is 1.

    The other template simply says that factorial of a number is that number times factorial minus 1.

    template<int T>
    struct factorial {
      enum { value = factorial<T - 1>::value * T };
    };
    

    Since there is really no “calling” in the classic sense, the template instantiates itself with a template argument equal to T-1 calculated at compile time.

    P.S. The warning shows that factorial of 34 overflows 32-bit integer.

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

Sidebar

Related Questions

I have the following regex in a C# program, and have difficulties understanding it:
could someone explain the code in catch_exceptions? i have difficulties to understand. thanks class
I have a bit of difficulties to understand memory issues with Android. The problem
I have difficulties calculating percentage of a double value. I wrote the code below
I'm working on Silverlight 4, and I have difficulties to make my combobox work
I have difficulties with how to use RLE on sequences of symbols. For example,
I got som difficulties to output HTML-string code through XML.I have presented a example
I am new in JavaScript and I have difficulties fixing this. There are three
I am reading the book: Intel Threading Building Blocks. I often have difficulties understanding
I have some difficulties understanding how the access method Do not move cursor automatically

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.