I'm trying to wrap my head around function templates. Specifically, why should function templates be declared and defined in their own header file?
Suppose you've decided to write a function template that you expect to use in many different source files. Suppose, further, that you haven't followed the basic advice of writing C++ templates in header files, but have instead done the following:
Written only a declaration of the function template in a header file x.hpp (i.e., all you wrote in the header file is template and then the function's signature). Written the function template, including its body, in the corresponding source file x.cpp. Now suppose that you've written a separate source file, y.cpp, in which you've written #include "x.hpp" near the top and then instantiated the function template by calling it, and that your call to the function template is a legal instantiation (i.e., it has the right number of parameters and they're compatible with any constraints introduced within the template).
Will it always be the case that this program can be built?
If so, why do we have a rule about writing C++ templates in header files (i.e., If this isn't the problem that rule solves, what problem does it solve?)? If not, specifically why does the program not build? What steps will succeed? What steps will fail?