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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:41:24+00:00 2026-05-24T20:41:24+00:00

I understand that templates kinda get blamed for binary bloat, I also understand that

  • 0

I understand that templates kinda get blamed for binary bloat, I also understand that a template is just a pattern. I dont really understand the nuts and bolts as it where.

Alot of time I see code like the following where it returns a base class pointer.

class GameObject
{
public:
    Component* getComponent(std::string key);
};

static_cast<Comp>(obj.getComponent("Comp"));

Rather than making the method a template method.

class GameObject
{
public:
    template<typename T>
    T* getComponent(std::string key);
};

obj.getComponent<Comp>("Comp");

Is this stylistic or is there a performance loss associated with the 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-24T20:41:24+00:00Added an answer on May 24, 2026 at 8:41 pm

    The fact that the method takes the “key” which appears to be really a type (the type of component to return) indicates to me that the type is not really known until runtime. If that’s the case then a compile time mechanism like templates will not do.

    The only option is to return a base class pointer. And typically when this pattern is used, only virtual methods are called against the base class–so the actual type of the derived class doesn’t matter (so no static_cast or dynamic_cast is needed).

    Edit:

    As PhilCK noted in comments, the type is actually known at compile time. If that is the case then dynamic type lookup was never really needed, and simple factory methods could have been used:

    class GameObject {
        A getComponentA();
        B getComponentB();
        C getComponentC();
        // etc.
    }
    
    // which is more or less identical to:
    
    class ComponentFactory {
        public:
        virtual Component* create() = 0;
    };
    class GameObject {
        std::map<std::string,ComponentFactory> m;
        public:
        GameObject() {
            // presumably a private map has to be populated with string -> factory methods here
        }
    
        template<class T>
        T* getComponent(const std::string& s) { 
            // either the static_cast is needed here, or an alternate (messier)
            // implementation of getComponent is needed.
            // it's still possible for the wrong type to be passed (by mistake)
            // and the compiler won't catch it (with static_cast). Since this could lead
            // to heap corruption the only safe way with this type of 
            // implementation is dynamic_cast.
            return static_cast<T>(m[s].create());
        }
    };
    
    // this doesn't even compile:
    // return types:
    class GameObject {
        template <class T>
        T* getComponent(const std::string& s) {
            if (s == "A") return new A();
            else if (s == "B") return new B();
            // etc..
            else throw runtime_error("bad type");
        }
    }
    

    So there are 2 choices the way I see it.

    1) use simple factory methods in which case templates are not needed at all.
    2) use map -> factory method implementation together with dynamic_cast (which seems to defeat the purpose of using dynamic type creation) and is actually unnecessarily complex if dynamic types aren’t needed

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

Sidebar

Related Questions

I understand that Microsoft uses this template when versioning their products: Major.Minor.Build.Revision. Major is
I understand that the Express edition doesn't template unit test projects... but being a
I understand that IronPython is an implementation of Python on the .NET platform just
Can anybody recommend a decent C# Mapper Pattern code generation template that plays nicely
I mean I understand that these templates are aimed at designers and other less
I understand that some countries have laws regarding website accessibility. In general, what are
I understand that there are several ways to blend XNA and WPF within the
I understand that they are both supposed to be small, but what are the
I understand that server-side validation is an absolute must to prevent malicious users (or
I understand that an id must be unique within an HTML/XHTML page. For a

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.