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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:20:09+00:00 2026-06-10T18:20:09+00:00

If I write a compile-time factorial function using specialisation, the following code can suffice,

  • 0

If I write a compile-time factorial function using specialisation, the following code can suffice, and will correctly provide 120 as the result of fact1<5>():

template <size_t N>
constexpr size_t fact1() { return N*fact1<N-1>(); }

template <>
constexpr size_t fact1<0>() { return 1; }

However, with a single function body and the ternary operator, as in the following code, G++ 4.7 and Clang++ 3.2 both exceed their maximum template instantiation depth. It appears that 1 is never returned from fact2. Why is it that this definition of fact2<5>() doesn’t return 120?

template <size_t N>
constexpr size_t fact2() { return N==0 ? 1 : N*fact2<N-1>(); }
  • 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-06-10T18:20:11+00:00Added an answer on June 10, 2026 at 6:20 pm

    The problem here is that no matter what, fact2<N-1> will always be instantiated (even non-executed paths need to be compiled, see Effective C++, I think Item 47 or 48). You need to, somehow, make it only instantiate the next function if you’re not at the end. One way would be to just say “screw templates” and go the usual constexpr way as @NicolBolas says in his comment.

    Another would be using one of the techniques used in this similar question.

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

Sidebar

Related Questions

I learned that Xcode can compile c code, that means can we write whole
Today, I found out that you can write such code in C++ and compile
I want to write a macro to write a string, using the compile-time optimisation
Hey all, I'm currently trying to write a compile-time string encryption (using the words
I'm trying to write a small application and have come across a compile time
When I code, I always write little pieces of unit, and compile it often.
Can I write a C++ code that can be compiled and used for extending
Possible Duplicate: Unit test compile-time error I'm wondering if its possible to write a
Here's an interesting problem. Is there a way to write some code using LINQ
I am currently trying to write some flexible compile time mathematics library and just

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.