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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:19:13+00:00 2026-05-22T20:19:13+00:00

Is there a way to implement a compile-time type dictionary via C++ templates? e.g.

  • 0

Is there a way to implement a compile-time type dictionary via C++ templates?
e.g. if I have a number of classes like these:

class ProtocolMajor1Minor2   { ... };
class ProtocolMajor4Minor3   { ... };
...
class ProtocolMajor12Minor21 { ... };

…is there a way to use C++ templates that would allow me to do something like this:

void foo(int majorVersion, int minorVersion)
{
    LookupMap<majorVersion,minorVersion>::innertype *specific = 
        new LookupMap<majorVersion,minorVersion>::innertype;
    return specific->FunctionalityFoo();
}

In case it is not clear, LookupMap acts just as the name says: given the two integer parameters (protocol major and minor versions) it is supposed to provide the specific protocol type I need, via the innerType “trait”.

I can’t use the preprocessor to create function “foo” as a macro (using ## or #), for two reasons: (a) it is big, not like in this example, and I don’t want a huge function coded as a macro, and (b) the naming mappings are not direct (i.e. major version A and minor version B do not point to class “ProtocolMajorAMinorB”.

You may also be thinking that “FunctionalityFoo” should be a member of a base type:
you are correct, but this is code generated from legacy code generators, i.e. untouchable.
There are in fact many functions like “FunctionalityFoo” generated for each combination of (major,minor), and I don’t want to create if/then/else ladders for each of them.

I tried template specialization but failed to find a syntax that works.

Is there a way to do it via templates?

  • 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-22T20:19:13+00:00Added an answer on May 22, 2026 at 8:19 pm

    No, templates are compile-time construct, so you cannot use variables as template arguments.
    For this to work you need to do something like this (i.e. not use runtime-provided variables):

    template <int A, int B>
    struct LookupMap { };
    
    template <>
    struct LookupMap<1, 2> { typedef Type1_2 innertype; };
    
    // ...
    template <int A, int B>
    sometype foo() {
        typedef typename LookupMap<A, B>::innertype T;
        T* ptr = new T; // also, this leaks, use a smart pointer or something
        return ptr->something();
    }
    
    // when used
    foo<1, 2>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to implement a URL mechanisim in asp.net like it has
Is there a way to implement a singleton object in C++ that is: Lazily
Is there a way to implement/use lambda functions in bash? I'm thinking of something
is there any way to implement timeout option for individual tests in a bunch
does anyone know is there a way to implement Windows Live ID authentication into
Is there a general way to implement part of an application with JavaScript and
I was wondering if there is a way to implement an action listener for
That's basically the question, is there a right way to implement operator<< ? Reading
Is there a way to use yield blocks to implement an IEnumerator<T> which can
Is there a way to find all web pages that implement a specific master

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.