1

In C, when you'd like to do generic programming, your only language-supported option is macros. They work great and are widely used, but are discouraged if you can get by with inline functions or regular functions instead. (If using gcc, you can also use gcc statement expressions, which avoid the double-evaluation "bug". Example.)

C++, however, has done away with the so-called "evils" of macros by creating templates. I'm still somewhat new to the full-blown gigantic behemoth of a language that is C++ (I assess it must have like 4 or 5x as many features and language constructs as C), and generally have favored macros or gcc statement expressions, but am being pressured more and more to use templates in their place. This begs the question: in general, when doing generic programming in C++, which will produce smaller executables: macros or templates?

If you say, "size doesn't matter, choose safety over size", I'm going to go ahead and stop you right there. For large computers and application programming, this may be true, but for microcontroller programming on an Arduino, ATTiny85 with 8KB Flash space for the program, or other small devices, that's hogwash. Size matters too, so tradeoffs must be made.

Which produces smaller executables for the same code when doing generic programming? Macros or templates? Any additional insight is welcome.

Related:

  1. Do c++ templates make programs slow?

Side note:

  1. Some things can only be done with macros, NOT templates. Take, for example, non-name-mangled stringizing/stringifying and X macros. More on X macros:
    1. Real-world use of X-Macros
    2. https://www.geeksforgeeks.org/x-macros-in-c/
    3. https://en.wikipedia.org/wiki/X_Macro
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 1
    Since it's going to depend on the specific macro, template, compiler and optimization settings - the only real answer is to try it and see. Although if templates turn out much bigger, come back and ask a question about whether you made a bad choice in the comparison. – Useless Apr 22 '20 at 23:41

2 Answers2

1

At this time of history, 2020, this is only the job of the optimizer. You can achieve better speed with assembly, the point is that it's not worth in both size and speed. With proper C++ programming your code will be fast enough and small enough. Getting faster or smaller by messing the readability of the code is not worth the trouble.

That said, macros replace stuff at the preprocessor level, templates do that at the compile level. You may get faster compilation time with macros, but a good compiler will optimize them more that macros. This means that you can have the same exe size, or possibly less with templates.

The vast, 99%, troubles of speed or size in an application comes from the programmers errors, not from the language. Very often I discover that some photo resources are PNG instead of proper JPG in my executable and voila, I have a bloat. Or that I accidentally forgot to use weak_ptr to break a reference and now I have two shared pointers that share 100MB of memory that will not be freed. It's almost always the human error.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • This is interesting: "A template does not produce smaller object code, though, compared to writing separate functions for all the different data types used in a specific program. For example, if a program uses both an int and a double version of the max() function template shown above, the compiler will create an object code version of max() that operates on int arguments and another object code version that operates on double arguments. The compiler output will be identical..." – Gabriel Staples Apr 26 '20 at 04:30
  • "...to what would have been produced if the source code had contained two separate non-templated versions of max(), one written to handle int and one written to handle double." [Wikipedia: Template (C++)](https://en.wikipedia.org/wiki/Template_(C%2B%2B)) – Gabriel Staples Apr 26 '20 at 04:30
0

... in general, when doing generic programming in C++, which will produce smaller executables: macros or templates?

Measure it. There shouldn't be a significant difference assuming you do a good job writing both versions (see the first point above), and your compiler is decent, and your code is equally sympathetic to both.

If you write something that is much bigger with templates - ask a specific question about that.

Note that the linked question's answer is talking about multiple non-mergeable instantiations. IME function templates are very often inlined, in which case they behave very much like type-safe macros, and there's no reason for the inlining site to be larger if it's otherwise the same code. If you start taking the addresses of function template instantiations, for example, that changes.

... C++ ... generally have favored macros ... being pressured more and more to use templates in their place.

You need to learn C++ properly. Perhaps you already have, but don't use templates much: that still leaves you badly-placed to write a good comparison.

This begs the question

No, it prompts the question.

Useless
  • 64,155
  • 6
  • 88
  • 132