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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:14:08+00:00 2026-06-05T08:14:08+00:00

Consider the following simple function void foo_rt(int n) { for(int i=0; i<n; ++i) {

  • 0

Consider the following simple function

void foo_rt(int n) {
    for(int i=0; i<n; ++i) {
        // ... do something relatively cheap ...
    }
}

If I know the parameter n at compiletime, I can write a template version of the same function:

template<int n>
void foo_ct() {
    for(int i=0; i<n; ++i) {
        // ... do something relatively cheap ...
    }
}

This allows the compiler to do things like loop unrolling, which increases speed.

But assume now that I sometimes know n at compiletime and sometimes only at runtime. How can I implement this without maintaining two versions of the function? I was thinking something along the lines:

inline void foo(int n) {
    for(int i=0; i<n; ++i) {
        // ... do something relatively cheap ...
    }
}

// Runtime version
void foo_rt(int n) { foo(n); }

// Compiletime version
template<int n>
void foo_ct() { foo(n); }

But I am not sure if all compilers are smart enough to deal with this. Is there a better way?

EDIT:

Clearly, one solution that will work is to use macros, but this I really want to avoid:

#define foo_body \
{ \
    for(int i=0; i<n; ++i) { \
        // ... do something relatively cheap ... \
    } \
}

// Runtime version
void foo_rt(int n) foo_body

// Compiletime version
template<int n>
void foo_ct() foo_body
  • 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-05T08:14:08+00:00Added an answer on June 5, 2026 at 8:14 am

    I’ve done this before, using a integral_variable type and std::integral_constant. This looks like a lot of code, but if you look again, it’s actually only a series of four very simple pieces, one of which is merely demo code.

    #include <type_traits>
    
    //type for acting like integeral_constant but with a variable
    template<class underlying>
    struct integral_variable {
        const underlying value;
        integral_variable(underlying v) :value(v) {}
    }; 
    
    //generic function
    template<class value> 
    void foo(value n) {
        for(int i=0; i<n.value; ++i) {
            // ... do something relatively cheap ...
        }
    } 
    
    //optional: specialize so callers don't have to do casts
    void foo_rt(int n) { return foo(integral_variable<int>(n)); }
    template<int n>
    void foo_ct() { return foo(std::integral_constant<unsigned, n>()); }
    //notice it even handles different underlying types.  Doesn't care.
    
    //usage is simple
    int main() {
        foo_rt(3);
        foo_ct<17>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following class: class Something : ISomething { public void DoesSomething(int x) {
Please consider the following simple use case: public class Foo { public virtual int
Consider following program: static void Main (string[] args) { int i; uint ui; i
Let us consider the following factorial example : #include <iostream.h> int factorial(int); void main(void)
Consider the following simple piece of code: %hash = ('a'=>1,'b'=>2); print $hash{'b'}; print \n,(\%hash)->{'b'};
Consider the following, simple class constructor. (note that I am obviously not including all
Consider the following: print 3 ** 333; #Yields 7.6098802313206e+158 My question is simple: How
Consider my first attempt, a simple type in F# like the following: type Test()
Consider the following sample program in which a loop is running; int main() {
Consider the following example: function async_callback(array1, array2, array3) { // All arrays are equal

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.