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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:13:26+00:00 2026-06-11T20:13:26+00:00

I want to implement a factory function for creating objects. My object template looks

  • 0

I want to implement a factory function for creating objects. My object template looks like this:

template <typename TA, typename TB>
struct MyImpl : public MyInterface
{
    // content
};

and my factory is like this:

MyInterface* factory(char ta, char tb)
{
    if(ta == 'c' && tb == 'c')
    {
        return new MyImpl<char, char>();
    }
    if(ta == 'c' && tb == 's')
    {
        return new MyImpl<char, short>();
    }
    if(ta == 's' && tb == 'c')
    {
        return new MyImpl<short, char>();
    }
    // and so on ....
}

The factory function must accept non-static char data (ta, tb) for it could not be determined at compile time, and I think that’s the whole point of this factory. In fact, ta and tb are read from a file (or network).

I want a simpler solution to avoid the annoying 2-level switch.

I think my question is similar to how-would-one-write-a-meta-if-else-if-in-c with the exception that I cannot use static parameters.

Perhaps I should just fallback to C macros and use some macro tricks to shrink my current code?

Thanks in advance!

UPDATE

Answer to @Rob:

My actual code would be more complex with many other stuffs in it and harder to read and not related in many aspects. I’m trying to get the pseudo-code right, If there is any problem, please kindly inform me :-).

Answer to @Dynguss:

My problem is that in my actual implementation, parameters of the factory (ta, tb) would be large in range, like 10 X ta and 20 X tb, and combination of ta and tb would be very long in lines, and hard to maintain. So I need at least some way to ease the combination efforts.

  • 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-11T20:13:27+00:00Added an answer on June 11, 2026 at 8:13 pm

    Here’s an idea:

    template <typename T>
    MyInterface * factroy(char t)
    {
        if (t == 'c') { return MyImpl<T, char>();  }
        if (t == 's') { return MyImpl<T, short>(); }
        // ...
    }
    
    MyInterface * factory(char ta, char tb)
    {
        if (ta == 'c') { return factroy<char>(tb);  }
        if (ta == 's') { return factroy<short>(tb); }
        // ...
    }
    

    With variadic templates, this pattern could be extended to any number of type arguments – for example:

    struct Base { virtual ~Base() = default; };
    template <typename A, typename B, typename C> struct Foo : Base { };
    
    #include <tuple>
    
    template <typename ...Args>
    constexpr Base * factory(std::tuple<Args...>)
    {
        return new Foo<Args...>;
    }
    
    template <typename ...Args, typename ...Char>
    constexpr Base * factory(std::tuple<Args...>, char t, Char ... ts)
    {
        return t == 'c' ? make(std::tuple<char,      Args...>(), ts...)
             : t == 's' ? make(std::tuple<short int, Args...>(), ts...)
             : t == 'i' ? make(std::tuple<int,       Args...>(), ts...)
             : t == 'l' ? make(std::tuple<long int,  Args...>(), ts...)
             : nullptr;
    }
    

    Usage: auto p = factory(std::tuple<>(), 'c', 's', 'l');

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

Sidebar

Related Questions

given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I'm working in an embedded environment (Arduino/AVR ATMega328) and want to implement the Factory
i starter in jqgrid, i want implement inline edit in jqgrid i have this
I want to use MEF with asp.net mvc. I wrote following controller factory: public
I am implementing something like an object factory in PHP. I have: class Factory{
I am training my template skills in C++ and want to implement a vector
I want implement in my software solution an VBA editor but in c# 3.0.
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement a simple script to do some boring housekeeping on my
I want to implement charting in SWT and I want to use GC.transform 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.