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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:04:40+00:00 2026-06-05T21:04:40+00:00

I am working with a library which exposes an interface to work with. One

  • 0

I am working with a library which exposes an interface to work with. One of the functions of this library is like this :

template <int a>
void modify(){}

I have to modify parameters from 1 to 10 i.e. call modify with with template arguments from 1 to 10. For that I wrote this code (a basic version of code, actual code is much larger).

for(int i = 0; i < 10; i++) {
    modify<i>(); // <<< error on this line
}

On compilation I receive the following error

error: 'i' cannot appear in constant-expression

After going through some links on the internet, I came to know that I cannot pass any value as template argument which is not evaluated at compile time.
My question are as follows:

  1. Why can’t compiler evaluate i at compile time?
  2. Is there any other to achieve the objective I am trying to achieve without changing the API interface?

There is another thing I want to do. Call modify as modify where VAR is the output of some functional computation. How can I do that?

  • 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-05T21:04:42+00:00Added an answer on June 5, 2026 at 9:04 pm

    What is the value of i (that is not a constant) at compile time? There is no way to answer unless executing the loop. But executing is not “compiling”
    Since there is no answer, the compiler cannot do that.

    Templates are not algorithm to be executed, but macros to be expanded to produce code.
    What you can do is rely on specialization to implement iteration by recursion, like here:

    #include <iostream>
    
    template<int i>
    void modify()
    { std::cout << "modify<"<<i<<">"<< std::endl; }
    
    template<int x, int to>
    struct static_for
    {
        void operator()() 
        {  modify<x>();  static_for<x+1,to>()(); }
    };
    
    template<int to>
    struct static_for<to,to>
    {
        void operator()() 
        {}
    };
    
    
    int main()
    {
        static_for<0,10>()();
    }
    

    Note that, by doing this, you are, in fact, instantiating 10 functions named
    modify<0> … modify<9>, called respectively by static_for<0,10>::operator() … static_for<9,10>::operator().

    The iteration ends because static_for<10,10> will be instantiated from the specialization that takes two identical values, that does nothing.

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

Sidebar

Related Questions

I have a third party library which I finally have working within my MonoTouch
I'm working on a thread library which implement user level threads (i have something
I have a class library which searches for an XML file in the working
I'm working on a library which uses jQuery internally which work fine. But I
I have been working on a project which uses Tiger's SQLite3 library (which if
I have been working for some time on a library which performs numeric calculations.
I have an integration test suite(which is a class library) and one of the
I'm working on a library which implements the actor model on top of Grand
I am working on a library which needs to make use of the common
My problem is this: I am trying to implement a C++ library which can

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.