#3 can be used to specialize template functions in one compilation unit without the need to update other compilation units.
Say we have z.cpp that looks like this:
template <class T> void foo (T*) { puts("1"); } template <class T> void foo (T) { puts("2"); } template <> void foo (int*) { puts("3"); } void foo(int*) { puts("4"); } int dummy() { foo((int*)NULL); foo<int>((int*)NULL); foo(4); foo((long*)NULL); }
and y.cpp , which looks like this:
#include <stdio.h> template <class T> void foo (T*); template <class T> void foo (T); int main() { foo((int*)NULL); foo<int>((int*)NULL); foo(4); foo((long*)NULL); }
The first lines of main refer to #3 , not #4 . This means that we can specialize foo for other types in z.cpp without changing y.cpp .
With #4 you will have to update y.cpp every time you add a new overload.
tohava
source share