6

Let us assume this small piece of code:

#include<iostream>

template <typename T>
class A {
  T a;
};

int main() {
  A<int> a;
  A<char> c;
}

Now, consider this code where instead of templates, I have two separate classes for int and char.

#include<iostream>

class A {
  int a;
};

class C {
  char c;
};

int main() {
  A a;
  C c;
}

Would there be any difference in the above two approaches as per compiler, optimization or code segment of the program?

In which approach executable size would be larger and why?

Hemant Bhargava
  • 3,251
  • 4
  • 24
  • 45
  • 9
    https://godbolt.org/ Compiler Explorer will show you the answer better than any of us can really tell you. – Mellester Feb 18 '19 at 11:26
  • 2
    gcc 8.2 spits out identical assembly code [template](https://godbolt.org/z/h9OZ9q), [non-template](https://godbolt.org/z/TU71EG). In general i'd prefer to use a template because it's more maintainable (less duplicate code, and less chance of redundant code down the line). – George Feb 18 '19 at 11:27
  • 2
    I'm pretty sure the result will be identical. – Dmytro Dadyka Feb 18 '19 at 11:29
  • 1
    The point is maintainability and readability. That's the balance to achieve and there are no simple right or wrong answers. Otherwise, the optimized code is probably identical in all instances. – Matthieu Brucher Feb 18 '19 at 11:29
  • A template means you don't need to also write `class D { double d; };`, and if you realize that you want a constructor you only need to add it once. – Daniel H Feb 18 '19 at 11:30
  • imho your second snippet is an absolute no-go because of code duplication, whether you resolve that via a template or other means depends on the specific case and is to a large extend a matter of taste – 463035818_is_not_an_ai Feb 18 '19 at 11:31
  • The "try-it-and-see" kind of answers will obviously be correct for this exact input, but you cannot infer from it whether templates always give bigger exe size or not without applying a bit of theory. – ROX Feb 18 '19 at 11:33
  • btw is it on purpose that in the second version the members are called differently? This is a small but significant difference as some other template using either of the two types now has to distinguish between them while in your template both have a member called `a` – 463035818_is_not_an_ai Feb 18 '19 at 11:34
  • @Mellester, Already did that experiment and updated the details in the below – Hemant Bhargava Feb 18 '19 at 11:47
  • @DmytroDadyka, I hope so.. :) – Hemant Bhargava Feb 18 '19 at 11:49
  • @MatthieuBrucher, I understand about the maintainability part but what about the problems where debug size can be huge due to cases like that? – Hemant Bhargava Feb 18 '19 at 11:49
  • @DanielH, That is true. But point of the question was to reduce the exec size. – Hemant Bhargava Feb 18 '19 at 11:49
  • `A` and `A` **are** two separate classes. – Caleth Feb 18 '19 at 11:55
  • Debug sizes would be the same, as @Caleth said. They are different classes. – Matthieu Brucher Feb 18 '19 at 16:52

2 Answers2

8

Templates are essentially a mechanism for source code generation, before the code is compiled.

The two approaches are identical from the perspective of code generation or executable size (except in the first case both classes get a member variable a, and in the second a and c).

Compare variant 1 with variant 2. Notice identical generated code.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • Thanks much for doing the experiment for me. Interesting thing to note is the size at both the versions. In first case, size is : 4067B and in another, size is : 3824B. Ring any bells? – Hemant Bhargava Feb 18 '19 at 11:46
  • Where do you see that? Object files will differ in size due to longer class names, but a release build with no debug info should be the same (with `g++ -O2` I got 8488 bytes in both versions) – rustyx Feb 18 '19 at 11:51
  • I see your point about passing -O2 flag. Do you think making a debug exec would be different between these two cases? I can't think of any reason. Do you? – Hemant Bhargava Feb 19 '19 at 04:50
1

Templates will be resolved at compile time based on the inputs present in the code, so executable size should be same in both the cases, unless there are some name differences present.

In your case, I think it should remain same.

Vijeth PO
  • 400
  • 1
  • 2
  • 19