I'm not entirely clear on how the new extern template
feature is meant to work in C++11. I understand that it is intended to help speed up compilation time, and simplify linking issues with shared libraries. Does that mean that the compiler does not even parse the function body, forcing a non-inlined call to be made? Or does it simply instruct the compiler to not generate an actual method body when a non-inlined call is made? Obviously, link-time code generation not withstanding.
As a concrete example of where the difference might matter, consider a function that operates on an incomplete type.
//Common header
template<typename T>
void DeleteMe(T* t) {
delete t;
}
struct Incomplete;
extern template void DeleteMe(Incomplete*);
//Implementation file 1
#include common_header
struct Incomplete { };
template void DeleteMe(Incomplete*);
//Implementation file 2
#include common_header
int main() {
Incomplete* p = factory_function_not_shown();
DeleteMe(p);
}
Within "Implementation file 2", it is unsafe to delete
a pointer to Incomplete
. So an inlined version of DeleteMe
would fail. But if it is left as an actual function call, and the function itself were generated within "Implementation file 1", everything will work correctly.
As a corollary, are the rules the same for member functions of templated classes with a similar extern template class
declaration?
For experimental purposes, MSVC produces the correct output to the above code, but if the extern
line is removed generates a warning about deleting an incomplete type. However, this is the remnants of a non-standard extension they introduced years ago so I'm not sure how much I can trust this behavior. I don't have access to any other build environments to experiment on [save ideone et al, but being limited to one translation unit is rather limiting in this case].