I’m creating a new container. One of its template parameters is its initial capacity. As a result, creating instances with different initial capacities will result in the compiler generating the same code for each templated class.
MyVector<3> firstVector;
MyVector<4> secondVector;
...
if (firstVector.empty() && secondVector.empty())
{
...
}
This snippet would cause the compiler to generate empty()
for both classes. Can a compiler eliminate this redudancy?
First of all, empty()
will perhaps be very simple and the compiler will be able to inline it. What if is cannot be inlined? The compiler can do any kind of merging but it must do it in such way that distinct functions have distinct addresses:
&MyVector<3>::empty() != &MyVector<4>::empty()
Visual C++ 10 can exhibit non-standard behavior here depending on linker settings - with some settings it will detect such functions and just merge them thus violating the Standard. I haven't seen it ever doing such elimination in a Standard-compliant way.