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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:15:17+00:00 2026-05-18T04:15:17+00:00

Some people here told it’s not possible and some people here told it might

  • 0

Some people here told it’s not possible and some people here told it might be possible to have one version of machine code for all different template arguments of a templated function, so I thought it would be profitable to open a thread regarding this specific matter, if there is not such possibility that’s it, if there is, in what situations can we count on this?
An example that was specifically considered in both mentioned threads is below:

template<size_t num>    
struct Elements{
 public:
  SomeType elements[num];
};

template<size_t num>
void print(const Elements<num> & elements,size_t size){
 //all instances do exactly same thing and with regard to Size that determines the size of object
}

and of course there’s pass by value version also:

template<size_t num>
void print(const Elements<num> elements,size_t size){
 //all instances do exactly same thing and with regard to Size that determines the size of object
}
  • 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-18T04:15:18+00:00Added an answer on May 18, 2026 at 4:15 am

    A smart linker can recognize when two different function bodies are identical and combine them together into one symbol. This is called “COMDAT folding” in MSVC and “duplicate stripping” most other places. For example, the following two functions might compile identically on PPC, although they take different types, because the types are the same size and behave identically in the given situation.

    template<typename T>
    GetLowBit( T foo ) { return T & 1; }
    
    GetLowBit<unsigned long>(ulong x); // compiles to "li r4, 1 ; and r3, r3, r4 ; blr "
    GetLowBit<signed long>(long x); // also compiles to "li r4, 1 ; and r3, r3, r4 ; blr "
    

    and so the linker can make both of their “names” point to the same place, as it were, so that a call to GetLowBit<unsigned long> goes to the same address as a call to GetLowBit<signed long>. So, in general, it’s possible to fold together different template instantiations of functions that all operate on identical type sizes in identical ways. (In particular, containers that store pointers or enums tend to get folded together.)

    This doesn’t just happen for template functions. Some linkers can notice when any two functions have identical bodies, and merge them. In particular I see that MSVC tends to collapse every virtual function that does nothing but return into one uber-function, eg for

    class A
    { 
      virtual void nothing() {};
    }
    
    class B
    {
       virtual void empty() {};
    }
    

    I often see (in the disassembler while debugging something else) that the vtable entries for nothing and empty both point to the same function body, which in turn is just a ret.

    Can you count on this? No. This is a feature that a smart linker might provide for functions when it notices them. Your linker might be stupid — there’s a lot of lousy compilers out there. It might only happen if you supply certain command arguments. It may happen for some functions but not others for reasons known only to the linker. It may even differ from build to build depending on what else is in the program.

    So in general, yes this can happen, and when it does, it can be a nice savings in executable size. But you can’t count on it happening unless you are very familiar with your linker and all its features.

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

Sidebar

Related Questions

Some time ago, people here helped me very well with code. Now I'm tasked
I have a app to manage book orders from some people, here is the
Some people have mentioned (e.g. here on StackOverflow ) the tofu scale when talking
I have some code here for a login system, that is purely or learning
Thanks to some help from people here, I was able to get my code
I have looked on here and found some people having some of the same
Very specific question but we have some good Ada people here so I would
When I read some of the answers on here I notice that some people
Just trying to canvas some opinions here. I was wondering how people go about
Some people have mentioned RockScroll and MetaScroll in This Question , but those only

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.