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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:12:27+00:00 2026-06-17T12:12:27+00:00

I am trying to use an integer as a template parameter for a class.

  • 0

I am trying to use an integer as a template parameter for a class. Here is a sample of the code:

template< int array_qty > 
class sample_class {

    public:
        std::array< std::string, array_qty > sample_array;

}

If I do so something like this, it works:

sample_class< 10 > sample_class_instance;

However, let’s say that I do not know the value of array_qty (the template parameter) when compiling, and will only know it during run-time. In this case, I would essentially be passing an int variable as the template argument. For the sake of demonstration, the following code does not work:

int test_var = 2;
int another_test_var = 5;
int test_array_qty = test_var * another_test_var;

sample_class< test_array_qty > sample_class_instance;

I get the following error during compile time when trying the above:

the value of ‘test_array_qty’ is not usable in a constant expression

I’ve tried converting test_array_qty to a const while passing it as the template parameter, but that doesn’t seem to do the trick either. Is there any way to do this, or am I misusing template parameters? Perhaps they need to be known at compile time?

The goal is NOT to solve this specific approach, but rather to find a way to set the length of the array to an int variable that can be stated when instantiating the class. If there is a way to do this via a template parameter, that would be ideal.

Please note that I have to use an array for this, and NOT a vector which I may end up as a suggestion. Additionally, array_qty will always be a value between 0 and 50 – in case that makes a difference.

  • 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-17T12:12:29+00:00Added an answer on June 17, 2026 at 12:12 pm

    This can be done in effect. But trust me when I say you are asking the wrong question. So what follows answers your question, even thought doing it is a bad idea almost always.

    What you in effect can do is create 50 different programs, one for each of the 50 possible sizes, and then conditionally jump to the one you want.

    template<int n>
    struct prog {
      void run() {
        // ...
      }
    };
    
    
    template<int n>
    struct switcher {
      void run(int v) {
        if(v==n)
          prog<n>::run();
        else
          switcher<n-1>::run(v);
      }
    };
    
    template<>
    struct switcher<-1> {
      void run(int v){
      }
    };
    

    Call switcher<50>::run( value ); and if value is 0 to 50, prog<value>::run() is invoked. Within prog::run the template parameter is a compile time value.

    Horrid hack, and odds are you would be better off using another solution, but it is what you asked for.

    Here is a C++14 table-based version:

    template<size_t N>
    using index_t = std::integral_constant<size_t, N>; // C++14
    
    template<size_t M>
    struct magic_switch_t {
      template<class F, class...Args>
      using R=std::result_of_t<F(index_t<0>, Args...)>;
      template<class F, class...Args>
      R<F, Args...> operator()(F&& f, size_t i, Args&&...args)const{
        if (i >= M)
          throw i; // make a better way to return an error
        return invoke(std::make_index_sequence<M>{}, std::forward<F>(f), i, std::forward<Args>(args)...);
      }
    private:
      template<size_t...Is, class F, class...Args>
      R<F, Args...> invoke(std::index_sequence<Is...>, F&&f, size_t i, Args&&...args)const {
        using pF=decltype(std::addressof(f));
        using call_func = R<F, Args...>(*)(pF pf, Args&&...args);
        static const call_func table[M]={
          [](pF pf, Args&&...args)->R<F, Args...>{
            return std::forward<F>(*pf)(index_t<Is>{}, std::forward<Args>(args)...);
          }...
        };
        return table[i](std::addressof(f), std::forward<Args>(args)...);
      }
    };
    

    magic_switch_t<N>{}( f, 3, blah1, blah2, etc ) will invoke f(index_t<3>{}, blah1, blah2, etc).

    Some C++14 compilers will choke on the variardic pack expansion containing a lambda. It isn’t essential, you can do a workaround, but the workaround is ugly.

    The C++14 features are all optional: you can implement it all in C++11, but again, ugly.

    The f passed basically should be a function object (either a lambda taking auto as the first argument, or a manual one). Passing a function name directly won’t work well, because the above best works when the first argument becomes a compile-time value.

    You can wrap a function template with a lambda or a function object to help.

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

Sidebar

Related Questions

Im trying to use an integer, that Im using in one class and also
I've been trying to use a PHP integer array for a MySQL query that
Newbie to python here, I am trying to use an integer as an argument
I am trying to use the code below to convert an integer in ax
I'm trying to use the function gethostbyname, but my code: int handleTCP(char *hostname, char*
I was just trying to use a void pointer to an integer array ,I
i am trying to use the following code... The Enum class i am using
I'm trying to use Boost regex to see if something has an integer in
How can I use a batch file to loop through non-integer values? I'm trying
I am need paint my image. I'm trying use JQuery in here this link:

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.