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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:42:05+00:00 2026-05-30T09:42:05+00:00

I wonder whether for long loops we can take advantage of tail recursion for

  • 0

I wonder whether for long loops we can take advantage of tail recursion for constexpr in C++11?

  • 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-30T09:42:07+00:00Added an answer on May 30, 2026 at 9:42 am

    By the rules in [implimits], an implementation is permitted to put a recursion depth limit on constexpr calculations. The two compilers which have complete constexpr implementations (gcc and clang) both apply such a limit, using the default of 512 recursive calls as suggested by the standard. For both of these compilers, as well as any other implementation which follows the standard’s suggestion, a tail recursion optimization would be essentially undetectable (unless the compiler would otherwise crash before reaching its recursion limit).

    An implementation could instead choose to only count calls for which it could not apply a tail-recursion optimization in its recursion depth limit, or to not provide such a limit. However, such an implementation would probably be doing a disservice to its users, since it would be likely to either crash (due to a stack overflow) or fail to terminate on constexpr evaluations which recurse deeply or infinitely.

    With regard to what happens when the recursion depth limit is reached, Pubby’s example raises an interesting point. [expr.const]p2 specifies that

    an invocation of a constexpr function or a constexpr constructor that would exceed the implementation-defined recursion limits (see Annex B);

    is not a constant expression. Therefore, if the recursion limit is reached in a context which requires a constant expression, the program is ill-formed. If a constexpr function is called in a context which does not require a constant expression, the implementation is not generally required to attempt to evaluate it at translation time, but if it chooses to, and the recursion limit is reached, it is required to instead perform the call at runtime. On a complete, compilable test program:

    constexpr unsigned long long f(unsigned long long n, unsigned long long s=0) {
      return n ? f(n-1,s+n) : s;
    }
    constexpr unsigned long long k = f(0xffffffff);
    

    GCC says:

    depthlimit.cpp:4:46:   in constexpr expansion of ‘f(4294967295ull, 0ull)’
    depthlimit.cpp:2:23:   in constexpr expansion of ‘f((n + -1ull), (s + n))’
    depthlimit.cpp:2:23:   in constexpr expansion of ‘f((n + -1ull), (s + n))’
    [... over 500 more copies of the previous message cut ...]
    depthlimit.cpp:2:23:   in constexpr expansion of ‘f((n + -1ull), (s + n))’
    depthlimit.cpp:4:46: error: constexpr evaluation depth exceeds maximum of 512 (use -fconstexpr-depth= to increase the maximum)
    

    and clang says:

    depthlimit.cpp:4:30: error: constexpr variable 'k' must be initialized by a constant expression
    constexpr unsigned long long k = f(0xffffffff);
                                 ^   ~~~~~~~~~~~~~
    depthlimit.cpp:2:14: note: constexpr evaluation exceeded maximum depth of 512 calls
      return n ? f(n-1,s+n) : s;
                 ^
    depthlimit.cpp:2:14: note: in call to 'f(4294966784, 2194728157440)'
    depthlimit.cpp:2:14: note: in call to 'f(4294966785, 2190433190655)'
    depthlimit.cpp:2:14: note: in call to 'f(4294966786, 2186138223869)'
    depthlimit.cpp:2:14: note: in call to 'f(4294966787, 2181843257082)'
    depthlimit.cpp:2:14: note: in call to 'f(4294966788, 2177548290294)'
    depthlimit.cpp:2:14: note: (skipping 502 calls in backtrace; use -fconstexpr-backtrace-limit=0 to see all)
    depthlimit.cpp:2:14: note: in call to 'f(4294967291, 17179869174)'
    depthlimit.cpp:2:14: note: in call to 'f(4294967292, 12884901882)'
    depthlimit.cpp:2:14: note: in call to 'f(4294967293, 8589934589)'
    depthlimit.cpp:2:14: note: in call to 'f(4294967294, 4294967295)'
    depthlimit.cpp:4:34: note: in call to 'f(4294967295, 0)'
    constexpr unsigned long long k = f(0xffffffff);
                                     ^
    

    If we modify the code so that the evaluation is not required to occur at translation time:

    constexpr unsigned long long f(unsigned long long n, unsigned long long s=0) {
      return n ? f(n-1,s+n) : s;
    }
    int main(int, char *[]) {
      return f(0xffffffff);
    }
    

    then both compilers accept it, and generate code which computes the result at runtime. When building with -O0, this code fails due to stack overflow. When building with -O2, the compilers’ optimizers transform the code to use tail recursion and the code functions correctly (but note that this tail recursion is unrelated to constexpr evaluation).

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

Sidebar

Related Questions

wonder whether someone can help me with the following one... I have a struct
I wonder whether I can setting up a private maven repository based on my
I wonder whether someone can help me please. I'm using the following script to
I wonder whether someone can help me please. I'm trying to put together a
I wonder whether someone could help me please. I've really got myself in a
I wonder whether someone may be able to help. I've created an image gallery
I wonder whether there is any automatic way of determining (at least roughly) the
I wonder whether someone may be able to help me please. I'm using Aurigma
I wonder whether someone could help me please. I'm using Image Uploader from Aurigma,
I wonder whether it is possible to use Django in-built comments framework for other

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.