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

  • Home
  • SEARCH
  • 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 815035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:37:03+00:00 2026-05-15T01:37:03+00:00

I have heard about code bloats in context of C++ templates. I know that

  • 0

I have heard about code bloats in context of C++ templates. I know that is not the case with modern C++ compilers. But, I want to construct an example and convince myself.

Lets say we have a class

template< typename T, size_t N >
class Array {
  public:
    T * data();
  private:
    T elems_[ N ];
};

template< typename T, size_t N >
T * Array<T>::data() {
    return elems_;
}

Further, let’s say types.h contains

typedef Array< int, 100 > MyArray;

x.cpp contains

MyArray ArrayX;

and y.cpp contains

MyArray ArrayY;

Now, how can I verify that the code space for MyArray::data() is same for both ArrayX and ArrayY?

What else I should know and verify from this (or other similar simple) examples? If there is any g++ specific tips, I am interested for that too.

PS: Regarding bloat, I am concerned even for the slightest of bloats, since I come from embedded context.


Addition: Does the situation change anyhow if the template classes are explicitly instantiated?

  • 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-15T01:37:04+00:00Added an answer on May 15, 2026 at 1:37 am

    You’re asking the wrong question – any “bloat” in your example has nothing to do with templates. (the answer to your question, btw, is to take the address of the member function in both modules and you’ll see they’re the same)

    What you really want to ask is, for each template instantiation, does the resulting executable grow linearly? The answer is no, the linker/optimizer will do magic.

    Compile an exe that creates one type:

    Array< int, 100 > MyArray;
    

    Note the resulting exe size. Now do it again:

    Array< int, 100 > MyArray;
    Array< int, 99 > MyArray;
    

    And so on, for 30 or so different versions, charting the resulting exe sizes. If templates were as horrible as people think, the exe size would grow by a fixed amount for each unique template instantiation.

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

Sidebar

Related Questions

I am referred to Hudson today. I have heard about continuous integration before, but
in the past few weeks I have heard about a phenomenon called 'code-kata'. When
Hi i have heard lot about using frameworks for creating native app .i want
Can I execute Python on Windows Phone? I have heard about IronPython but is
I have heard about evil and based HTML, but I have never seen any
Good evening, I have been trying to wrap my head about this, below, code
I have heard about JNI and have tried to use it. What I would
I have heard a lot about anti patterns and would like to read a
I have heard positive things about Eclipse RCP (Rich Client Platform) - does Microsoft
New to PHP and MySQL, have heard amazing things about this website from Leo

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.