Suppose I have the following sample code just to instantiate the insert method of std::vector
for at least two trivial types:
#include <vector>
void insert(std::vector<int>& v, int const* a, int const* b)
{ v.insert(v.end(), a, b); }
void insert(std::vector<short>& v, short const* a, short const* b)
{ v.insert(v.end(), a, b); }
If you compile this code, you will get nearly identical copies of the same code (see how it compiles here).
Is it possible to achieve more compact code without rolling your own specialized std::vector which assumes that T is trivial? So it is possible to throw away the type in the true implementation, so the heavy work can be implemented by using memcpy
and realloc
directly. I guess this solution according to the standard is UB.
As an example, the compiler has not merged the two instantiations of uninitialized_copy
. I realize that it is not allowed to map two different symbols to the same address, but couldn't it at least replace the second copy with jump to the first. This does not even happen with -Os
.