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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:35:25+00:00 2026-05-27T04:35:25+00:00

It is often said that the code with lots of templates is going to

  • 0

It is often said that the code with lots of templates is going to cause the output to increase in size, but is it really true?

#include <iostream>

#if 0
void foo( const int &v)
{
    std::cout<<v<<std::endl;
}
#else
template< typename T >
void foo( const T &v)
{
    std::cout<<v<<std::endl;
}
#endif

int main ()
{
    foo(50);
}

The example above produces outputs of different sizes (6.19k with the function, and 6.16k with a template function). Why is the version with a template smaller?

If it matters, I am using g++ 4.6.1, with next options -O3 -Wextra -Wall -pedantic. I am not sure what is the output of other compilers.

  • 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-27T04:35:25+00:00Added an answer on May 27, 2026 at 4:35 am

    Perhaps because foo in your example has external linkage so that it is emitted into your executable even if the call is inlined.

    For the template, if the call is inlined there is no reason to emit an implicitly instantiated function template specialization.

    Try making foo an inline function or making it static. If you want to emit the function template specialization you need to explicitly instantiate it

    #else
    template< typename T >
    void foo( const T &v)
    {
        std::cout<<v<<std::endl;
    }
    template void foo(const int&);
    #endif
    

    Doing this, my measures give the exact same size for the non-template function and the function template version.

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

Sidebar

Related Questions

It seems to be often said that Task Manager does not provide an accurate
Often, programmers write code that generates other code. (The technical term is metaprogramming ,
I come from a python background, where it's often said that it's easier to
Possible Duplicate: What is a translation unit in C++ It is often said that
It is often said when unit testing to dont test the database as that
Often I find myself interacting with files in some way but after writing the
Often multiple applications share a large codebase of libraries that change often in development.
It seems that often I will want to execute some methods from a Class
While Joel Spolsky thinks that every bug should be fixed before new code is
So in the following bit of code, I really like the verbosity of scenario

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.