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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:53:17+00:00 2026-05-24T18:53:17+00:00

Is it possible to decide in run-time which template function to call? Something like:

  • 0

Is it possible to decide in run-time which template function to call?
Something like:

template<int I>
struct A {
    static void foo() {/*...*/}
};

void bar(int i) {
    A<i>::f();   // <-- ???
}
  • 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-24T18:53:17+00:00Added an answer on May 24, 2026 at 6:53 pm

    A typical ‘trick’ to bridge compile time and runtime when dealing with templates is visiting a variant type. That’s what the Generic Image Library (available as Boost.GIL or standalone) does for instance. It typically takes the form of:

    typedef boost::variant<T, U, V> variant_type;
    variant_type variant = /* type is picked at runtime */
    boost::apply_visitor(visitor(), variant);
    

    where visitor is a polymorphic functor that simply forwards to the template:

    struct visitor: boost::static_visitor<> {
        template<typename T>
        void
        operator()(T const& t) const
        { foo(t); } // the real work is in template<typename T> void foo(T const&);
    };
    

    This has the nice design that the list of types that the template will/can be instantiated with (here, the variant_type type synonym) is not coupled to the rest of the code. Metafunctions like boost::make_variant_over also allows computations over the list of types to use.

    Since this technique is not available to non-type parameters, you need to ‘unroll’ the visitation by hand, which unfortunately means the code is not as readable/maintainable.

    void
    bar(int i) {
        switch(i) {
            case 0: A<0>::f(); break;
            case 1: A<1>::f(); break;
            case 2: A<2>::f(); break;
    
            default:
                // handle
        }
    }
    

    The usual way to deal with the repetition in the above switch is to (ab)use the preprocessor. An (untested) example using Boost.Preprocessor:

    #ifndef LIMIT
     #define LIMIT 20 // 'reasonable' default if nothing is supplied at build time
    #endif
    #define PASTE(rep, n, _) case n: A< n >::f(); break;
    
    void
    bar(int i) {
        switch(i) {
            BOOST_PP_REPEAT(LIMIT, PASTE, _)
    
            default:
                // handle
        }
    }
    
    #undef PASTE
    #undef LIMIT
    

    Better find good, self-documenting names for LIMIT (wouldn’t hurt for PASTE either), and limit the above code-generation to just one site.


    Building from David’s solution and your comments:

    template<int... Indices>
    struct indices {
        typedef indices<Indices..., sizeof...(Indices)> next;
    };
    
    template<int N>
    struct build_indices {
        typedef typename build_indices<N - 1>::type::next type;
    };
    
    template<>
    struct build_indices<0> {
        typedef indices<> type;
    };
    
    template<int... Indices>
    void
    bar(int i, indices<Indices...>)
    {
        static void (*lookup[])() = { &A<Indices>::f... };
        lookup[i]();
    }
    

    then to call bar: bar(i, typename build_indices<N>::type()) where N would be your constant-time constant, sizeof...(something). You can add a layer to hide the ‘ugliness’ of that call:

    template<int N>
    void
    bar(int i)
    { bar(i, typename build_indices<N>::type()); }
    

    which is called as bar<N>(i).

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

Sidebar

Related Questions

I have seen an implementation of Factory using static methods. Something like this: public
is it possible to decide at runtime which properties of a JS object should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
How is it possible to run tests against a class that reads from System.in
If I write this: class Program { static void Main(string[] args) { throw new
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on

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.