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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:33:00+00:00 2026-05-14T00:33:00+00:00

I have heard from many people that usage of templates make the code slow.

  • 0

I have heard from many people that usage of templates make the code slow. Is it really true. I’m currently building a library. There are places where if templates are not created, it would result in code management problem. As of now I can think two solutions to this problem:

  • use #defines

  • Use templates and define all possible types in the header file/library itself but do not allow end user to make template instances.

e.g. typedef Graph<int> GraphI32; etc.

Is there anyway, to restrict user from creating various template instances on their own.

Help on above queries would be highly regarded.

  • 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-14T00:33:00+00:00Added an answer on May 14, 2026 at 12:33 am

    The short answer is no. For the longer answer please read on.

    As others have already noted, templates don’t have a direct run-time penalty — i.e. all their tricks happen at compile time. Indirectly, however, they can slow things down under a few circumstances. In particular, each instantiation of a template (normally) produces code that’s separate and unique from other instantiations. Under a few circumstances, this can lead to slow execution, by simply producing enough object code that it no longer fits in the cache well.

    With respect to code size: yes, most compilers can and will fold together the code for identical instantiations — but that’s normally the case only when the instantiations are truly identical. The compiler will not insert code to do even the most trivial conversions to get two minutely different instantiations to match each other. For example, a normal function call can and will convert T * to T const * so calls that use either const or non-const arguments will use the same code (unless you’ve chosen to overload the function on constness, in which case you’ve probably done so specifically to provide different behavior for the two cases). With a template, that won’t happen — instantiations over T * and T const * will result in two entirely separate pieces of code being generated. It’s possible the compiler (or linker) may be able to merge the two after the fact, but not entirely certain (e.g., I’ve certainly used compilers that didn’t).

    But in the end, templates have positive effects on speed far more often than negative.

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

Sidebar

Related Questions

i have heard from a friend of mine that the best algorithm for swapping
I have heard that DateTime.Now is very expensive call (from here ) Is GETDATE()
You may have heard of PawSense , a Windows-only utility that prevents keystrokes from
I realized that many of my past projects have suffered from too much ajax.
I have found Design patterns certification at the Brainbehch. I have heard from people
Is it possible? From what I have heard it is. I wouldn't mind just
New to PHP and MySQL, have heard amazing things about this website from Leo
HI, I have this query about smart pointers. I heard from one of my
I have heard that when dealing with mutexes, the necessary memory barriers are handled
I'm really interested in becoming a serious programmer, the type that people admire for

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.