3

If I use a template class to create 30 different definitions. My question is that will it compile into 30 actual classes in binary (binary size = sizeof(template_class) x 30), although their actual code are very similar or even exactly the same ?

And if it would, and during runtime, my program is load into memory. I loop through those 30 instances (assume i initialized 1 instance per definition), would it causes the cpu instruction cache to reload because they are actually 30 copies in memory, even most of their code are the same?

    template<typename msg_policy, int id>
    class temp_class_test : public msg_policy
            //codes, methods, and members
    };

    template class temp_class_test<A_policy,1>;
    template class temp_class_test<B_policy, 2>;
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Bryan Fok
  • 3,277
  • 2
  • 31
  • 59
  • 1
    duplicating code is never a good idea, already for code maintainance. Consider a design were you re-use code (by using inheritance, for example), when the issue you're worried about would be avoided, too. – Walter Mar 18 '13 at 10:54
  • 5
    @Walter That's nonsense. The whole point of templates is to avoid code duplication. On the other hand, inheritance should be used for polymorphic behavior, not for code reuse. – jrok Mar 18 '13 at 10:56
  • But would the above code be translated into two complete new binary set of temp_class_test? – Bryan Fok Mar 18 '13 at 10:58
  • 1
    @jrok what is nonsense? to not duplicate code? Of course you can use inheritance for that. Suppose, for example, that `temp_class_test

    ` is the *same* except for the value of `id`, then you only need to define `temp_class_test

    ` in detail and can declare `template class temp_class_test

    : public temp_class_test

    { /* constructors */ };\

    – Walter Mar 18 '13 at 11:02
  • 1
    @BryanFok I think this is implementation dependent. most likely it will. – Walter Mar 18 '13 at 11:03
  • @Walter Apoologies if I misunderstood you, but I disagree with the idea to use inheritance on its own to avoid code reuse where templates would do the job. Example in your second comment - I believe that's a different thing and yes, I'm fine with that. – jrok Mar 18 '13 at 11:10
  • 1
    "will it compile into 30 actual classes in binary (binary size = sizeof(template_class) x 30), although their actual code are very similar or even exactly the same ?" - Similar but not "exactly the same". Yes. This is one of the causes of "code bloat". – SChepurin Mar 18 '13 at 11:21

3 Answers3

2

As it stands, the classes instantiated from your template are distinct classes with distinct code. And yes, that would cause the code to be reloaded again and again at execution.

You can mitigate this the way Walter suggested in the comments: by having the implementation in some common base class that all other classes derive from. I went into some detail about this technique in my answer on the question on “How to define different types for the same class in C++”.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

If your 30 odd classes are very similar, you should avoid code duplication. For example, the template parameter id may just be a stub to differentiate otherwise identical classes. In this case, you don't need to re-define the class for every id, but may simply inherit from an implementation:

namespace implementation {
  template<typename p> class test {
    // implementation
  };
}
template<typename p, int id>
class temp_class_test : public implementation::test<p>
{
   // any additional code dependent on id
   // any non-inheritable code (constructors)
};

The compiler will then only generate one binary for each method of the base class and the id merely serves to distinguish different temp_class_test.

Walter
  • 44,150
  • 20
  • 113
  • 196
0

Autopsy done. That is why my application perform so badly when my application is under stress. It was because my cpu was experienced forever cache miss.

Bryan Fok
  • 3,277
  • 2
  • 31
  • 59