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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:30:34+00:00 2026-05-30T08:30:34+00:00

I have a set of functions that are templated both by an integer type

  • 0

I have a set of functions that are templated both by an integer type Index and a class type T, that I “partially specialize” in the following manner:

// Integer type
enum Index{One,Two,Three,Four};

// Default implementation
template<int I>
struct Foo{
  template<typename T> static void bar(const T& x){ std::cout <<"default" << endl; }
};

// Template specializations
template<> 
struct Foo<One>{
  template<typename T> static void bar(const T& x){ std::cout << "one" << endl; }
};

This I use to select a particular index at the runtime of the program using a switch-statement (which should result in an efficient look-up table). The switch is independent of T:

template<typename T>
void barSwitch(int k, const T& x){
  switch(k){
    case ONE: Foo<ONE>::bar(x); break;
    case TWO: Foo<TWO>::bar(x); break;
    case THREE: Foo<THREE>::bar(x); break;
  }
}

This works fine, of course, but the class Foo is not the only class for which I would like to apply the switch. In fact, I have a lot of classes that are all templated by the same integer type. So I would like to “template” the class barSwitch above with the function “Foo” as well, so that I can plug in a different class or a different function. The only way I can think of to achieve this is to use a macro:

#define createBarSwitch(f,b) \
template<typename T> \
void barSwitch(int k, const T& x){ \
  switch(k){ \
    case ONE: f<ONE>::b(x); break; \
    case TWO: f<TWO>::b(x); break; \
    case THREE: f<THREE>::b(x); break; \
  }\
}

Is there some better, more C++ style way of doing this?

  • 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-30T08:30:36+00:00Added an answer on May 30, 2026 at 8:30 am

    Template template parameters are the key:

    enum Index { One, Two, Three, Four };
    
    template <template <Index> class Switcher, typename T>
    void barSwitch(int k, const T & x)
    {
        switch (k)
        {
            case 1: Switcher<One>::template bar<T>(x); break;
            case 2: Switcher<Two>::template bar<T>(x); break;
            default: assert(false);
        }
    }
    

    Usage:

    template <Index I> struct Foo
    {
        template <typename T> static void bar(const T & x);
    };
    
    barSwitch<Foo>(1, Blue);
    

    (It is your responsibility to ensure that every possible template that you substitute for Switcher has a member template bar, of course. If not, you’ll get a compile error.)

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

Sidebar

Related Questions

Using VBA i have a set of functions that return an ADODB.Recordset where all
I have a problem with a javascript set of functions that I made. This
I have certain initializing functions that I use to set up audit logging on
I want to make sure that a set of functions have the same signature
I have a set of templates/functions that allow me to print a tuple/pair assuming
I have the following PLSQL functions that returns following error when returning a large
I have a template class that has lots of functions but is essentially a
I am adding musicCD information to a set. I have two different functions for
I'm hopeless at Javascript. This is what I have: <script type=text/javascript> function beginrefresh(){ //set
I have a templated function fct that uses some complex data structure based 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.