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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:42:51+00:00 2026-05-17T21:42:51+00:00

Can I use template to create several instantiations of some function, different only in

  • 0

Can I use template to create several instantiations of some function, different only in some constant parameter? The number of alternatives for this parameter is fixed.
E.g.

I want not to rewrite (where upper is in 1..32 in powers of two)

funct(param, int upper)
 {
  some_loops(..)
     some_heavy_code_fast_for_const_and_slow_for_variable(upper)
 }

into a set of

funct_with_upper_is_1(param) // upper =1
 { manually_copied_code...heavy(1) }
funct_with_upper_is_2(param) // upper =2
 { manually_copied_code...heavy(2) }
funct_with_upper_is_4(param) // upper =4
 { manually_copied_code...heavy(4) }
funct_with_upper_is_8(param) // upper =8
 { manually_copied_code...heavy(8) }

but into templated

template<int upper>
 funct_with_fixed_upper(param)
 { the_original_code....heavy(upper) }

and then

template<upper=1> funct_with_fixed_upper(param);
template<upper=2> funct_with_fixed_upper(param);
template<upper=4> funct_with_fixed_upper(param);
template<upper=8> funct_with_fixed_upper(param);

Is this possible with C++ tempaltes?

== Verbose mode on ==

I have a lot C++ files with code like that

function_typical(long long double ***A, long long double ***B, int const_1, int const_2)
// the type "long long double" here is very correct, this is extension of compiler
{

   for(int i=1;i<100000-1;i++)
      for(int j=1;j<100000-1;j++)
         for(int k=0;k<const_1;k++)
            for(int l=k;l<const_2;l++) {
                // some cray work with array like
                A[i][j][l-k]+=(B[i][j][l-k]+A[i+1][j][l-k]+A[i][j+1][l-k]-A[i-1][j][k]-A[i][j-1][l-k]/2.2)/88.3;
                if(A[i][j][l-k]>sin_lld(A[i][j-1][l-k])){B[i][j][l-k]=A[i][j][k]*4;}
            }
 }

This is just an example, but:

  • I can’t interchange the loops;
  • the 2 outer loops, i & j have a lot of iterations
  • the 2 inner (nested), k& l have a bit of iterations, number of which is passed into the function_typical and the set of them are fixed, e.g. const_1 and const_2 is one of pairs: (2,3), (4,5), (3,5). Total number of allowed pairs is smaller then 10.

The problem with this code is its speed is very low. If I will fix const_1 and const_2 in this code to numberic constants, compiler will do a great job in optimizing (e.g unrolling all k and all l iterations, doing a some smart job).

But I physically can’t change every typical-like function of every set of (const_1 and const_2) pair. Also, constant propagator of compiler can’t propagate constants of the set info the function (this is a networking server and the client does a selection of some const_1 and const_2 pair form fixed set).

So I know in compile-time all the alternatives of pairs. But I have no chance of rewrite every function by hand.

== verbose mode off ==

Thanks in advance

  • 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-17T21:42:52+00:00Added an answer on May 17, 2026 at 9:42 pm

    Absolutely, this is entirely possible. If you take an int by template, it is valid wherever a constant expression is valid.

    template<int const_1, int const_2> function_typical(long long double ***A, long long double ***B)
    // the type "long long double" here is very correct, this is extension of compiler
    {
    
       for(int i=1;i<100000-1;i++)
          for(int j=1;j<100000-1;j++)
             for(int k=0;k<const_1;k++)
                for(int l=k;l<const_2;l++) {
                    // some cray work with array like
                    A[i][j][l-k]+=(B[i][j][l-k]+A[i+1][j][l-k]+A[i][j+1][l-k]-A[i-1][j][k]-A[i][j-1][l-k]/2.2)/88.3;
                    if(A[i][j][l-k]>sin_lld(A[i][j][l-k])){B[i][j][l-k]=A[i][j][k]*4;}
                }
     }
    

    This should re-compile straight, but you’ll have to alter the call sites. Also, don’t forget that templated code has to have the full source in all translation units and can’t be defined in just one.

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

Sidebar

Related Questions

I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
Can I create parametrized control template, i.e. create control template which might contain different
I come from a C++ background where I can use template mixins to write
In Django's template language, you can use {% url [viewname] [args] %} to generate
In VB.NET is there a library of template dialogs I can use? It's easy
You can use App.config; but it only supports key/value pairs. You can use .Net
how can I use the same UI template (*.ui.xml file) with multiple Java objects
I'm trying to create a simple qDebug-like class I can use to output debug
You can use more than one css class in an HTML tag in current
You can use a standard dot notation or a method call in Objective-C to

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.