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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:00:18+00:00 2026-05-13T23:00:18+00:00

According to answers to that question: Which, if any, C++ compilers do tail-recursion optimization?

  • 0

According to answers to that question:
Which, if any, C++ compilers do tail-recursion optimization?
it seems, that compiler should do tail-recursion optimization.

But I’ve tried proposed options and it seems that compiler can’t do this optimization in case of template functions. Could it be fixed somehow?

  • 1 1 Answer
  • 1 View
  • 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-13T23:00:18+00:00Added an answer on May 13, 2026 at 11:00 pm

    I don’t use the MS compilers, but GCC can certainly do tail-recursion optimisation for templates. Given this function:

    template <typename T>
    T f( T t ) {
       cout << t << endl;
       if ( t == 0 ) {
          return t;
       }
       return f( t - 1 );
    }
    

    The code produced is:

        5   T f( T t ) {
        6       cout << t << endl;
    -   0x401362    <main+22>:      mov    %esi,0x4(%esp)
    -   0x401366    <main+26>:      movl   $0x4740c0,(%esp)
    -   0x40136d    <main+33>:      call   0x448620 <_ZNSolsEi>
    -   0x401372    <main+38>:      mov    %eax,%ebx
        7      if ( t == 0 ) {
    -   0x4013a5    <main+89>:      test   %esi,%esi
    -   0x4013a7    <main+91>:      je     0x4013c8 <main+124>
        8         return t;
        9      }
        10     return f( t - 1 );
    -   0x4013a9    <main+93>:      dec    %esi
    -   0x4013aa    <main+94>:      jmp    0x401362 <main+22>
        11  }
    

    You can see that the recursive call has been turned into a jump back to the start of the function. This optimisation is only performed by GCC if the code is compiled with optimisations enabled (-O2 in this case) – perhaps the same is true for MS C++?

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

Sidebar

Related Questions

I posted this question on the Qt forum, but got no answers. That's why
According to the answers to another question , the VB user interface cannot be
According to the answers to this question, I cannot embed a file version in
According to this answer , I can make C++ mode think that underscores are
According to my previous question integration of jsf2.0 and spring 3.1 and hibernate 4.1
I've noticed that there are a couple of similar questions and answers at SO
(I've edited this question to avoid distractions. There is one core question which would
This might be a noob question, but can't find an answer anywhere. I have
This question will undoubtedly be difficult to answer and ask in a way that
I have a homework and I need to evaluate which approach is better according

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.