3 ms·
> I'm sure vectors of ints is quite common. I wish C++ could share that generated object code (not the instantiated template at compile-time). [...] I wish the
by plorkyeran 11y ago
> I'm sure vectors of ints is quite common. I wish C++ could share that generated object code (not the instantiated template at compile-time). [...] I wish the compiler could aggressively look for and link to instances that have the same binary representation - even if they have disparate type information.
Today your wish comes true, because this is something that compilers do and have done for a very long time. Redundant definitions of templated functions which didn't get inlined always get dropped at link time. VC++ and the Gold linker also have a feature called "Identical COMDAT Folding" which will agreesively merge functions with different names but identical bodies, which can merge distinct template instantiations in the case where the concrete type doesn't effect the compiled representation.
extern templates are orthogonal to this whole thing; they're purely a compile speed optimization and have no effect on the end binary after linking.
- coroutines 11y ago> Redundant definitions of templated functions which didn't get inlined always get dropped at link time. I wonder how many things aren't aggressively inlined in C++.. --- Are you saying `extern template` is used like a `#pragma once` just for the template? I thought it had a greater effect on linking. :( My concern is that even if the generated object code for std::vector<int> and a binary-compatible type like std::vector<something> are deduplicated in your codebase, that same object code for that specialized type of vector probably exist in several different programs throughout my system. (as an example)
- pjmlp 11y agoThat is implementation specific behavior and changes even between versions of the same compiler. Has it should be, actually. Freezing inlining algorithms in a language specification hinders future optimization algorithms. Chandler Carruth has done quite a few interesting talks how the clang inliner works.