5

Templates and inline functions should be provided in the header, so the same code will go inside each translation unit that uses them.

Unless the compiler chooses to actually put the template or inline code in-line, it would be nice for the linker to remove the same code appearing in each translation unit. Does this actully happen? required by the standard? or up to the compiler?

2 Answers2

3

First of all there's no such thing as the linker in the standard, so if you're looking at the standard you have to call it the implementation (which is responsible for turning the source code into an executable in some sense). IIRC there's a requirement that the same template in different translation units should have the same address (but I may be mistaken). Inline function otoh is the same as a static function, just allowed to be declared in every translation unit without complaint (note that the compiler is not required to actually inline the inline function or forbidden to inline other functions).

However normally a linker will do just what you suggest, namely to put only one instance of the same object in the executable (under this circumstance). There are features in the language that relies on the linker being able to do that anyway (IIRC) so there's no reason why the linker shouldn't be that smart anyway.

Then it's good question if you should rely on this fact in your code. Granted it would be nice if the executable were as small as possible, but other than that? How often do you compare if function f1 is the same as function f2 or in other way relies on that (f1 and f2 being pointer to the samely named function retrieved from different translation units)? If you require the executable to be small, you should of course select an implementation (compiler+linker) that produces just that.

skyking
  • 13,817
  • 1
  • 35
  • 57
0

inline functions are only suggestion we can ask (follow the defined rules for inline functions), compiler to look at (different compilers have different criteria for handling inline functions).

Now templates! Templates need two phase compilations and they are not treated as inline functions.

vector v1<int>; 
vector v2<int>; 

both statements require two phase compilations (v1 and v2 have same type, but only we know it, not the compilers). Different optimizations may have been applied but this is general working method for template compilations

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Ali Kazmi
  • 1,460
  • 9
  • 22
  • How come the compiler can't know that `v1` and `v2` have same type? Are there situations when that is not true? – skyking Aug 05 '15 at 06:26
  • types of template variables are known at runtime. while execcuting that statement – Ali Kazmi Aug 05 '15 at 06:32
  • Yes, but that didn't answer my question. Note that the compiler may also now the types (for example it have to determine which overloaded function to use or whether there exist one at all or it's ambigous). The question is why the compiler can't know it in this case and if there are situation when that's not possible (to know they're of the same type). – skyking Aug 05 '15 at 07:01
  • compiler knows that `v1` and `v2` are same type, as you can do `static_assert(std::is_same::value, "v1, v2 should be same type");` – Jarod42 Aug 05 '15 at 09:35